I see my recently graduated friends solves consumer problem by the procedural way (writing everything in a single class or a method) using object-oriented programming language.
In this post, I am sharing what and how I think while solving the consumer problem, The object-oriented thinking!
IMO, as an object-oriented designer, I divide the big problem into smaller ones. I provide the solution for small problems (concerns) using objects. Eventually, these objects communicate with each other by sending messages to solve the consumer’s big problem.
Let me explain it with the help of the use case. (We will consider the server-side solution)
As the new user of the system, I want to register a user account.
We would consider only two fields on the user registration page, Name, and Email and sign up button.
User can create his/her account
- As a user, I navigate to the registration page.
- Enter name and email.
- Choose to register.
- The user account is created.
- Email is sent to the recipient.
The user cannot create an account when a name is not provided.
The user cannot create an account when email is not provided.
As a developer, I need to provide the solution for these use cases. (I know there could be many alternate cases, however, let’s keep it simple.)
Clearly, there are different concerns that need to be addressed.
IMO, we need a class:
- To validate the user inputs.
- To create a record in a database.
- To generate email content.
- To send user registration email.
Thus, I would divide the concerns into
- UserValidator class that validates user inputs and gives an error for validation rules like the name is required, email is required and so on.
- UserRepository class that takes user entity and insert records into the database.
- EmailTemplate class that generates the email content.
- EmailSender class that takes email content and sends the email to the recipients.
UserController class that does the following stuff and try to solve the big problem by sending messages to the object of the above classes.
- Verify the user inputs.
- Create a user in the database.
- Get registration email content.
- Send registration email.
Note: To convey the intent, I kept this design as simple as possible. We could give more clean solutions like outsourcing the responsibilities of the controller (Keep controllers as thin as possible) to model classes and so on. Please feel free to share your feedback with me.
You can observe, The proper object-oriented design would divide the big problem into the smaller ones across classes. Solve small problems in order to solve the bigger one.