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

[3046 byte] By [laslos2] at [2007-9-27 18:48:43]
# 1
i briefly looked through your solution. you didn't describe the unique characteristics of the composite class.-which is the "component"?-which are the "leaf"?-etc.you might want to take a look at the Template pattern.
Poop at 2007-7-6 20:11:42 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

Your problem is the apparent conflicting thoughts on the 'shape' of data your data structure. Whilst it certainly appears hierarchic at first which suggest the composite pattern the latter restrictions suggest it is really a network.

I can see the following possible solutions.

1) Use a resource pool to find/construct and return each leafs this 'tags' each record and tracks those returned by a current reference for duplicate leafs.

2) Construct your data as a network, then overlay a composite as an access/indexing mechanism, make each leaf a reference to the correct node in the network rather than making it a data node.

Depending on the nature of your system you may need use both these and depending on the amount of duplication the overhead of these solutions may be worse than the duplication, you will need familiarity with the data set to make this deduction.

Another issue you need to consider is are your nodes static or generic. i.e. Do you have Cache,SuperCompany,Company,User,etc Classes; or are these classifications (i.e an Attribute) of a generic CompositeNode. If you use the solutions above them adopt the latter.

MartinS. at 2007-7-6 20:11:42 > top of Java-index,Other Topics,Patterns & OO Design...