This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Selecting a helper class based on object type. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Selecting a helper class based on object type." Watch "Selecting a helper class based on object type." New topic
Author

Selecting a helper class based on object type.

Matt Morten
Greenhorn

Joined: Jul 08, 2009
Posts: 7
I have the following classes:

Model Objects
Animal
Cat extends Animal
Dog extends Animal

Service Objects
ObjectDAO

Factory Objects
DataHelperFactory

Helper Objects
AnimalDataHelper
CatDataHelper extends AnimalDataHelper
DogDataHelper extends AnimalDataHelper

My ObjectDAO object will construct SQL using the various helpers. Ie, to retrieve a list of Cats, you must use the CatDataHelper.

However, I want to keep this as decoupled as possible:

1) ObjectDAO should call DataHelperFactory to get the correct Handler. It will pass in the given model (eg, Cat.class) and will receive the appropriate handler (eg, a new CatDataHelper)
2) Cat should not need to know about CatDataHelper
3) I would prefer not to do instanceof checks inside DataHelperFactory.

What is the best way to accomplish this?

Thanks!
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
What is the difference between Cat and CatDataHelper? What is a "helper" class?
Matt Morten
Greenhorn

Joined: Jul 08, 2009
Posts: 7
It is just a seperation of concerns.

The Cat object holds only information relevant to the Cat. Eg, name, colour, breed .....

The CatDataHelper contains the logic which allows a Cat to be persisted correctly. This logic could be quite complicated. Whats more, the Cat shouldn't need to know how it is put into a DB, nor should it know how it is drawn to screen, sent over a network, serialized to file, converted to XML etc.... Hence, I've taken the persistence code away from the Cat, and into the CatDataHelper.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Sounds good. Keep in mind that business model objects contain data AND behavior that executes business logic with the data. This is encapsulated within a single class. The logic that you describe mostly is tied to persistence so it does not belong in the business model class. Since the "helper" class contains logic and is mostly related to persistence, I would have named the class CatDAOImpl instead of CatDataHelper.
Matt Morten
Greenhorn

Joined: Jul 08, 2009
Posts: 7
Thanks, but I'm not sure that answers my question.

How do I ensure the code selects CatDataHelper based on the object type it has been given. The easiest way is probably:



The problem with this is two fold:

1) Not very flexible
2) The service object (ObjectDAO) is now dependent on all the different animal types. I would like to keep it as abstract as possible.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Your interpretation of what "flexible" means in terms of your code and what you think is possible is unknown.

If this code fragment is part of the ObjectDAO class and it is to be used to create instances of your helper classes, then it must know about every class that it will create and will also need to know what condition will determine the runtime execution path, i.e. what helper object to create. This leads to questioning the purpose of the DataHelperFactory.

An alternative would be to define a permanent code which could be used to control the conditional logic instead of using instanceOf operator.






Aside, it is best to not be distracted by abstract theories about coupling and abstraction and focus instead on creating the most simple and easy to understand code as possible. EASY and SIMPLE is better than DIFFICULT and COMPLEX. In many cases individuals fail to create simple code because they are not focused on the "big picture" or don't have the engineering skills to create simple code which executes complex logic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Selecting a helper class based on object type.
 
Similar Threads
Object casting
Covariant returns
ClassCastException
a strange question about polymorphism
instanceof operator