• Post Reply Bookmark Topic Watch Topic
  • New Topic

Single Responsibility Principle  RSS feed

 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a baseball player object which has a separate field for at-bats, hits, walks, singles, doubles, triples and home-runs. MLB's website does not list singles as a separate stat so I have to calculate singles from the rest of the data. I have been up-dating the singles field in every method where one of the other fields is updated but this seems to violate the SRP. But if I don't do it that way then everyplace in the code I change a player field I will have to remember to recalculate the Single field, eg. any code which uses setWalks(int walks) will also have to use calculateSingles(). That seems like an easy way for errors to appear, but I assume it will be easier to maintain since the calculateSingles() method call will be more obvious. If it makes any difference, changing a player field will happen a lot and the user will not want to have to hand-calculate how that effects the single field. Any feedback on the best way to handle this would be appreciated.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why do you actually need a singles field? I am assuming that all of your fields are private -- and you are using setters and getters? If so, then for singles, don't use a field, and have the getter calculate the value to be returned.

Henry
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Why do you actually need a singles field? I am assuming that all of your fields are private -- and you are using setters and getters? If so, then for singles, don't use a field, and have the getter calculate the value to be returned.

Henry

That works. Thanks!
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could make all these independent fields properties.
Add a propertylistener in which you recalculate all the dependent
fields.

That might indeed not be very useful, but it adds to your experience.
Look at the section about Listeners in the Oracle tutorials.

Another way would be to create some form of the publisher-subscriber
model. Do you work with Java 8?

Greetz,
Piet
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:You could make all these independent fields properties.
Add a propertylistener in which you recalculate all the dependent
fields.

That might indeed not be very useful, but it adds to your experience.
Look at the section about Listeners in the Oracle tutorials.

Another way would be to create some form of the publisher-subscriber
model. Do you work with Java 8?

Greetz,
Piet


I have not worked with Java 8 yet. Is Listeners part of Java 8?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Property listeners have been around for a long time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!