In the world of software development, SOLID principles tell us how to organize functions and data so that our codes:
The term SOLID is an acronym for five design postulates, described below:
(S) Single Responsibility Principle: "A module must have one, and only one reason to change"
(The) Open/Closed Principle: "A software artifact must be open for extension but closed for modification"
(L) Liskov Substitution Principle: "A derived class must be replaceable by its base class"
(I) Interface Segregation Principle: "A class should not be forced to implement interfaces and methods that it will not use"
(D) Dependency Inversion Principle: "Depend on abstractions and not implementations"
SOLID is designed for object-oriented programming, and it is known that GoLang is not a language that adopts this paradigm. However, we can use the resources it makes available to meet the OOP methodology. For example, Go does not have inheritance support, but the idea can be compensated with its composition support. Similarly, a type of polymorphism can be created using interfaces.
In this article, the first in a series of 5, I intend to detail the first principle with examples that are close to situations we encounter on a daily basis.
We already know what the term means, now it's time to learn how to implement it in GoLang.
In this language, we could define this principle as “A function or type must have one and only one job, and one and only one responsibility”, let’s see the following code:
Above, we have a struct we call userService. It has two properties: db, which is responsible for communicating with a relational database, and amqpChannel
, which enables communication with the RabbitMQ messaging service.
UserService implements a method called Create. Within this method we store the received user information in the database and then publish the data to RabbitMQ.
This can lead to several problems such as:
In the following code, we modified the structure to respect the SRP. Check it out:
Note that we have separated the responsibilities into three distinct parts: the repository UserRepository to persist the user to the database, the publisher UserPublisher to send a message to RabbitMQ, and the service UserService that orchestrates these two operations.
In this way, each component is responsible for a specific and independent task, facilitating the maintenance and evolution of the code, in addition to allowing each of these parts to be replaced or improved without affecting the others. For example, if it is necessary to change the database used, simply replace the repository. If it is necessary to change the form of communication, simply change the publisher.
It is worth noting that there is a subtle difference between performing two distinct tasks and delegating their execution. In the original userService.Create example, two operations were performed in one place, violating the principle of single responsibility. After refactoring, we delegated executions to different structs and the Create method was only responsible for coordinating this flow.
To apply SRP in this example, we also ended up implementing some of the other SOLID principles:
In the next articles in this series I will provide a more detailed explanation of each of them, with specific examples.
See you later, guys!
References:
SOLID: The First 5 Principles of Object Oriented Design
Clean Coder Blog - The Single Responsibility Principle
The above is the detailed content of Princípios SOLID em GoLang - Single Responsability Principle (SRP). For more information, please follow other related articles on the PHP Chinese website!