OOP – What is the example of a single responsibility?

Can someone give me an example of the single responsibility principle? I am trying to understand that in practice, a class has a single responsibility because I am worried that I may violate this rule every day.
View the Solid description.

Unless you ask for something more specific, it is difficult to provide more help.

Single responsibility is for one class to do one specific thing (Responsibility) instead of trying to do more concepts, this is also called high cohesion.

Classes usually don’t start with low cohesion, but are usually added in several versions and different developers After them, suddenly you will notice that it has become a monster or god-level, as some people say. So the course should be refactored.

It’s hard to think of a good example, but What I can think of recently is a class that manages different packet processing stages, a kind of chain of responsibility. The original intention of this class is to maintain a list of stages and coordinate the packetProcess() that calls them. Well, it finally shows that everyone is processing The stage (because the manager class is an easy place to access the stage) adds anything related to this manager class, especially the stage configuration. The manager class no longer has a single responsibility, but is responsible for calling the stage for configuration changes: So Cohesion Has been reduced.

We finally had to refactor the manager class, delete all stage configurations and put them into the factory, so that the manager can do what it wants.

Can someone give me an example of the single responsibility principle? I am trying to understand that in practice, a class has a single responsibility because I am worried that I may violate this rule every day.

Check the Solid description.

< /p>

Unless you ask for more specific things, it is difficult to provide more help.

Single responsibility is the concept of a class doing a specific thing (responsibility) rather than trying to do more things , This is also called high cohesion.

Classes usually don’t start with low cohesion, but usually after several versions and different developers join them, suddenly you will notice that it becomes A monster or god level, as some people say. So the course should be refactored.

It’s hard to think of a good example, but what I can think of recently is one that manages different packet processing The stage class, a kind of responsibility chain. The original intention of this class is to maintain a list of stages and coordinate the packetProcess() that calls them. Well, it finally shows that everyone is in the processing stage (because the manager class is an easy way to access the stage Place) added anything related to this manager class, especially the stage configuration. The manager class no longer has a single responsibility, but is responsible for invoking the stage to make configuration changes: so Cohesion has been reduced.

We finally Had to refactor the manager class, delete all stage configuration and put it into the factory, so that the manager can do what it wants to do.

Leave a Comment

Your email address will not be published.