Composite pattern & Caching.
Hi there!
Once again a problem to solve.
Im currently developing a application with a problem domain that easily could be designed with the composite pattern (Hier structure).
The domain consist of the following entities;
Company; As the name says, an organizational unit.
Supercompany; Virtual company that consists of one or several other companies or other Supercompanies. As the patterns recomends Im having the following classes;
Cache - Runtime cache holding variouos BusinessPartner objects.
User - An application user that always has a relationship to one BusinessPartner.
Company - see above.
Supercompany - see above.
AbstractCompositeCompany - Abstract class containg a data structure with references to other BusinessPartner objects.
Businesspartner; Abstract super class for all other classes defined above.
A application user (implemented as aUser object) has always a reference to aBusinessPartner (Supercompany or a singleCompany).
When a user are logging in to the application the application code should be able to resolve the user objects reference to theBusinessPartner.
I want to cache theBusinessPartner objects in som LRU cache and thats just my problem (Not the cache). Imagine the following scenario;
UserU1 are logging in, the user belongs to aCompany objectC1, so some control object ask磗 the cache for theCompany object. IfC1 is not in the cache, the cache dispatches the work to theCompany object磗 registred DAO class (Data Access Object). TheCompany object are now in the cache (Resolved from a database).
UserU2 loggs in, this user has a relationship to aSupercompanySC1, this instance consist of 2 otherCompany objects, lets sayC1 andC2.
TheSC1 object are not in the cache so it has to be resolved from the database. The cache know "asks" the DAO class to find it. Here磗 the problem, I don磘 want to resolve the Company objectC1 once again because he is already in the cache. Should theSuperCompany DAO class only set the attributes that are specific to theSC1 object and let the cache completet the "instantiation" of theSC1, that is to set the references to the belonging Company objectsC1 andC2?
I磎 not sure if Ive succed in presenting my problem in understandable way, if not please say so!!
What I don磘 like is that In the "suggestion" above is that the responsibiltiy of instantiating aSuperCompany object are "scatterd" / shared between diffrent objects (TheSuperCompany DAO class & theCache object).
Does anyone having a good soulution to my problem. Have looked at the factory pattern but I can磘 see how this could help me in the given situation.
Best regards laslos

