• Post Reply Bookmark Topic Watch Topic
  • New Topic

Separation of concerns  RSS feed

 
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a User has a List of Favorites, does the responsibility of adding and removing a favorite belong to the User class itself, or to an outside class that manages the list of users?

My colleague told me that data type classes such as User should never contain logic and that it should be placed in an external class that manages the User. I think this makes sense in an extensibility point of view because you would then not be able to expand since your logic is now stuck inside (tied to) the data class.

What do you think?
 
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also think it is the responsibility of outside classes.
Let me put my understanding here.

If a User has a List of Favorites, User class should be a POJO and we never put any logic in POJO.

Let us wait for the experienced guys what they have to say on this.
 
Ryan McClain
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like it didn't happen, Mr. Chand.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is the User class a "data type" class? As for the responsibility of adding and removing it from a list, don't fall into the trap of modeling your software like real-world objects. An abstract concept like a Favorites class could conceivably be the most appropriate one to assign that responsibility. I could see this code as being valid:


You have to "feel" your way around these decisions, try a few things and see which ones work. Use SOLID design principles and GRASP to guide you.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And don't forget to consider the context in which these objects will be used. Don't just model software and make design decisions without a well-defined context. A test-driven approach to design and development helps you define that context quickly and safely. I highly recommend learning and using the TDD approach.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:If a User has a List of Favorites, User class should be a POJO and we never put any logic in POJO.

This is totally not true. Of course POJOs can have logic. In fact, I would argue that you should think long and hard about why you would create a class that is pure "data" and no logic. Read up about the Anemic Domain Model and make informed decisions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!