This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
(this is my first post here so I appologize if this has already been discussed before)
I have an app where each user of the app are encapsulated in a User class. The problem is that certain functionality (such as searching for a user) does not require every single attribute of a user. As a result, I'm using objects that are much "heavier" than they should be for certain cases. I could create a LightWeughtUser class to use in these cases, but I'd like something a little more elegant. I don't want to end up down the road (as features are added to the app) with a LightWeightUser, a HeavyUser, MediumUser, etc. Anyone know of a pattern or have ideas on how to solve this? Ideally, I'd like a sitution where I can just "grow" the object as needed. Thanks in advance for any help...just don't want to reinvent the wheel. [ June 10, 2004: Message edited by: Michael J. Makunas ]
I think an example or two would probably help us to grasp what you need a bit more easily. I'm somewhat puzzled by (for example) whether you are saying that your system has different types of users with different (or optional) information and responsibilities, or whether you are implying that in your application design there is some external processing code which somehow needs to know what is "inside" a user object.
Could you give an example of what your guess at a "light weight" and "heavy weight" user might look like, and tell us a little more about the code that uses them?
This is what I mean by lightweiht vs. heavyweight user objects:
On this web based app someone can log in and serach for other users based on certain criteria (age, location, etc.). The data access layer does the db query and I get back a list of uids. I then use the list of uids to create a collection of ueser object (User user1 = new User(uid)) For the purposes of displaying search results, all I need is first and last name (user1.getFName() and user1.getLName()). But let say that users have a popularity attribute (user1.getPopularity()) that it calculated by some resource intensive algorithm. I don't want to calculate popularity if I don't need to. There may be other attributes that be resource intensive to calculate as well. Different parts of the app may or may not need them, hence varying degrees of "heavyness".
I think I've answered my own question, though. There is no need for the User class constructor to initialize everything. I can just have the constructor do the minimal stuff (first name, last name) and the the call to User.getPopularity check to see if popularity has already been calculated and if not, do the heavylifting itself.
Thanks for your help. I think I was just way over analyzing the problem. [ June 10, 2004: Message edited by: Michael J. Makunas ]