• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

where to put behavior

 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 @ https://coderanch.com/t/441195/patterns/Observer-Pattern-Push-versus-Pull
 
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic