Classes Should Know Their Objects Pattern

Work in progress.

PROBLEM: It is 3am in the morning who knows where your objects are?

CONTEXT: Outside of some simple software an identifier is used, but inside we need to pass the object arround. What should be responsible for knowing what objects exist and help us find one?

FORCES:

SOLUTION: Make the class responsible. Have static/classwide/metaclass data listing the objects by identifier and a static/classwide/metaclass function that finds them. Constructors and destructors add/remove objects from the list of existing objects.

VARIATION: If there is a Factory that constructs/deletes objects then make it responsible for managing identities.

RESULTING CONTEXT: We can send the class itself a request to find an object and from then on use the object it returns.

EXAMPLEs:

Login provides an id and a password for a Person (say). Send the id to the Person class and get a Person back (or NULL). If not null, send the password to the object to be authenticated and get the Person back or a NULL... CONTRAINDICATIONS: No polymorphic static methods. May not have remote invocation of static methods. What about the security implications of a metaclass knowing identifiers and objects? --SamuelFalvo?

ALTERNATIVES:

RELATED PATTERNS AlwaysEncryptPasswordsPattern?, IdentifersToObjectsPattern?.

Author: RichardBotting, March 7th 2008. Revized June 18th 2009.


Discussion

Recognizing that this is a WorkInProgress, I couldn't help but make some observations. Not sure where else I may post this.

CORBA uses a dedicated naming service to locate objects. I prefer the naming service approach because the factory (or other agent) publishes only those objects which anyone can access. I am not a fan of ClassesShouldKnowTheirObjectsPattern because of its security implications, and it appears to break the goal of separation of concerns. If your code lacks a proper reference to an object, the probability that said code has been authorized to hold that reference approaches, or even equals, zero.

I would like to see a bit on the security implications of this pattern.

Also, there is the (smaller) issue of CodeReuse; is this pattern best provided by the language, a library of some sort, or will this tend to be custom-grown on a per-class, or at least per-3rd-party-package, basis? --SamuelFalvo?

Thanks for caring... I've rolled some of your comments into the pattern spec. Note -- I posted this here because I am in doubt about the idea and want to see it run the gauntlet of intelligent review -- RichardBotting


EditText of this page (last edited June 18, 2009) or FindPage with title or text search