This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes where to put behavior Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "where to put behavior" Watch "where to put behavior" New topic

where to put behavior

Hendra Kurniawan
Ranch Hand

Joined: Jan 31, 2011
Posts: 239
Where is it better to put non setter and getter methods? in the class itself or in other class like this:


because lately, I've encountered a lot of design of behaviorless class.
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

The first version would normally be preferable. You are also probably right in suspecting that there is a problem with a design that has a lot of data-only classes.

Junilu - [How to Ask Questions] [How to Answer Questions]
Sameer C Naik

Joined: Apr 18, 2010
Posts: 7
To take further design discussion,
As you have specified non setter/getter methods. I am interpreting it as interface methods which requires access to data/behavior of class There are two types of interface, push and pull interface. if pull-style interface for example web browser data, i.e. you desire information, you type URL and in response web page data shown. otherwise in push-style interface just transfer data to you, say Observer pattern.
You can use either style or both, depending on your design.
for e.g. in case of Collection object, both-style can work as follows,
pull-style: a for iterate collection object of cars for its each element as, car.startEngine();
push-style: for each element in 'otherObject', the startEngine() method calls property of car, i.e through otherObject.startEngine(car);

more detail @
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 128

I would vote for the first approach, if you have multiple entities which you expect to have the startEngine() method, you can have an interface or abstract class (as per your need) like Vehicle or MachineWithEngine which has startEngine() method & then have your Car class extending/implementing it.
I agree. Here's the link:
subject: where to put behavior
It's not a secret anymore!