Design Patterns – Designing an Online Book Reader

design-patternsdomain-driven-designobject-orientedobject-oriented-design

I want to design an online book reader system as a programming exercise. I wrote down the following requirements:

  • Searching a book by title.
  • Bookmarking a book.
  • Mark a book as favorite.
  • Give rating.
  • Manage users.

The constraints are that only one user can read a particular book at a time.

I follow Clean architecture. Which advocates that:

  • Defer implementation details like DB and delivery mechanisms.
  • Start with the use case first which guides Interactors / Service Objects.
  • In designing use cases you will come across different Entities.
  • Don't pass objects between boundaries.

I know that question seems too broad hence but I wanted to proper context first, I will proceed from first step. I would like to know to pick up which use case first? Second, should I formulate all Service Objects and then move on to Entities?

Best Answer

Learn to model a simple problem into a problem domain model and then into code.

Your first four requirements all relate to books. Your final "Manage users" is vague, so I'll leave that for now.

How do you represent a Book? Assuming each book is unique, you model it with TWO objects. One being a particular book (Book), and the other the set of all books (Library). Library has a list of Book. Book belongs to Library.

Favorite, Bookmark and Rate become methods on Book. After all you should know the rule:- "An action on a real-world object, becomes a method of the object in object-oriented code".

Search becomes a method on the Library object. Add the ability to add, edit and delete book/s in the Library object also.

Create an object that can persist (and search etc) your library to a file, DB or morse code. Create objects that represents human interaction, like a window etc that calls methods on your domain.

Now you have a working system. For now, forget Clean/Service Objects/Interactors/Entities/Boundaries/SomeOtherSilverBullet. They are all rubbish in the way to you producing a simple solution to your problem.