aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Lightweight vs. Heavyweight objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Lightweight vs. Heavyweight objects" Watch "Lightweight vs. Heavyweight objects" New topic
Author

Lightweight vs. Heavyweight objects

Michael J. Makunas
Ranch Hand

Joined: Mar 11, 2002
Posts: 37
(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 ]
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
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?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Michael J. Makunas
Ranch Hand

Joined: Mar 11, 2002
Posts: 37
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 ]
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Glad I could help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lightweight vs. Heavyweight objects