This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

where to put behavior

 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sameer C Naik
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic