How it should be designed a book object in the Amazon website?
Should it have a logic?
if I were the architect of the system I wouldn’t have put any logic in that book object so it should be just a container and below are my reasons
- The clean code says that one class should be as short as possible with just one responsibility. the less methods and the less code is inside the better it is.
- I imagine the Amazon web site made of the Model View Control pattern and SQL database using the DAO pattern. If that’s implemented using also Spring and the Dependency injenction pattern and Hibernate the system should be designed as below
- View: normal HTMLÂ Javascript page so when the user wants to see the list of the book it calls the REST endpoint in the Spring book controller called getBook
- Controller: Spring Book controller with the initial business logic for verifying the input of the user, calling the model, populating a response object which contains a list of books and returns it to the view
- All logic should be in the controller and its method getBooks, the book should contain just informations
- Model: Another REST endpoint which like the controller has the logic. The logic is inside the rest book service and the book object should be just a container.
- That rest service can collect the information from the SQL database using hibernate and as usual the Hibernate book object is just a mapping of the book table, all logic should be inside the BookDAO using the DAO design pattern
For me the book should be just a bean POJO with private variables, getters, setters and constructors
The more logic you put inside the less clean is the code