Design principle: single responsibility principle

Design principle: single responsibility principle

Overview

Single responsibility means that a service is only responsible for the functions closely related to its own function, and does not need to do things that other services should do; for example, commodity services only care about products related to goods, and do not need to do things related to order services at the same time.
Similarly, a method in a class should only do one thing.

importance

Single responsibility principle can make code maintainability and improve code reusability

Show

Student A wrote a method methodA{
methodB();
}
Student B wrote a method methodB{
methodC();
}
Student C wrote a method methodC{
do someThing;
}At
this time, student D does not know the implementation logic inside , Student D only needs method A and method B. As a result, he wrote a method methodD{
methodA();
methodB();
}
There is a problem at this time, and the calling path becomes: A->B->C->B ->C, B and C are repeated in the call chain.
Make improvements: methodA{do something}, methodB{do something}, methodC{do something}. At this time, the call chain becomes A->B. In the
development of this logic, it is especially important to note that a method only does one thing.

to sum up

Single responsibility can improve the reusability of the code and reduce the coupling of the code, which is worth noting in the development and design.

Guess you like

Origin blog.51cto.com/xxdeelon/2539780