This is a common design pattern, but what is its name, and are its pros/cons discussed in any open literature? What do you think the pros/cons are?
// get a token object that identifies this registration
var registration = Registry.register("Foo", ...);
// unregistration accomplished via the token object
registration.unregister();
As opposed to:
Registry.register("Foo", ...);
Registry.unregister("Foo", ...);
Or even:
var registration = Registry.register("Foo", ...);
Registry.unregister(registration);
4
It isn’t a pattern, but an anti-pattern, in that the API requires the declaring scope to manage the token and will leak the registration token (and probably the registered object too) if you fail to do so properly. This could be remedied through the use of a destructor function for the object.
But given the sample code appears to be Javascript, which has no such facility, you have to manage the object’s registration yourself.
4
The code you’ve provided isn’t clearly or unambiguously an example of a token “pattern.” What you’ve provided just looks like “object oriented programming.”
Tokens are special objects (usually strings) used for identification or authorization when connecting to another service (or process, or namespace). And in those cases, there isn’t a singular pattern name I’d use. You’re simply “using a token.”
You could dig a little deeper and name purpose of the token, like a session token or authorization token. But, it’s not really a formal “pattern” per se. It’s just, “using a token.”
3