Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Eray Kalkan
Eray Kalkan
2,477 Points

Design approach question. (Calling addReview method inside Course.java)

Hi,

In Building a REST API in Spring Course on topic Populating and Relationships; Isn't calling the method addReview inside an entity (Course.java) a violation to single responsibility principle? Shouldn't we call it on an upper layer like a service class or something similar? Or is this a common approach while cascading two entities? Your kind assistance would be appreciated.

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Hey Eray,

I think I see what you're saying: a Review and Course are two separate things as they are two separate classes. If we were only to look at it from that perspective, then it would seem odd that a Course contains code that handles Review objects.

But, let's remember that the Course class exists to retrieve the state of Course objects (various getters) and change the state of Course objects (various setters).

Within each Course object is a List of Reviews (or rather, since this is an abstraction of database relationships, each record in the Course table can have many records in the Review table). So, the List of Reviews is a part of the Course object's state.

With that in mind, the Course class isn't violating the Single Responsibility Principle, because the addReview method exists to update/change the state of the Course object. It's just doing that by updating the related collection of Reviews.

I know it can be a little confusing at first.

Let me know if you have any more questions!

1 Answer

Eray Kalkan
Eray Kalkan
2,477 Points

Hi Chris,

Thank you very much for your reply.

The relationship seemed vague at first. My brain wanted to think there is an indirect relationship between those objecs but at the same time it also thought why would they have such a link. That's why it caused confusion. However, after your clarification it is clear for me now.

Kind regards, Eray