aspose file tools*
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
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "where to put behavior" Watch "where to put behavior" New topic
Author

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:

or


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

Joined: Feb 26, 2001
Posts: 5288
    
  10

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
Greenhorn

Joined: Apr 18, 2010
Posts: 5
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 @ http://www.coderanch.com/t/441195/patterns/Observer-Pattern-Push-versus-Pull
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 124

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: http://aspose.com/file-tools
 
subject: where to put behavior