Defining a class is only responsible for one responsibility
Responsibility diffusion
What is called the spread of responsibility, is the responsibility of further refinement , take the company, like the needs of customers, demand is constantly changing, and there is a great deal of uncertainty, maybe one day boss find you want to re-refine a demand
So it's best to refactor the code immediately before it spreads to the point where we can't control it.
Chestnuts
Let's start with the following code:
Class Animal: def __init__ (self,name): self.name = name def breathe (self): print ('%s breathes with lungs '%self.name ) Niu = Animal (' small cow ') Zhu = Animal (' Little white Pig ') niu.breathe () zhu.breathe ()
We will soon be able to find some logical errors from the above code, because not all animals are breathing with the lungs, and some use the gills to breathe, should be subdivided into terrestrial animal species terrestrial, aquatic animal aquatic, the code is as follows: Animal
Class Animal: def __init__ (self,name): self.name = Nameclass Terrestrial (Animal): def Breathe (self): print ('%s lungs Breathe '%self.name) class aquatic (Animal): def Breathe (self): print ('%s with gills breath '%self.name) c = Terrestrial (' Kitten ') c.breathe () fish = aquatic (' mermaid ') fish.breathe ()
We see that the implementation of the above code is the animal class out of two classes, and the method of breathing written into the derived class, good! This is no problem, so take a look at the following code:
Class Breathebehavior: def Breathe (self):p assclass lungbreathe (breathebehavior): def Breathe (self): Print (' breathing with lungs ') class cheekbreathe (Breathebehavior): def Breathe (self): print (' breath with Gills ') class Animal: def __ Init__ (Self,name,breathep): self.name = name Self . Breatheparam = Breathep def performbreathe (self): self . Breatheparam.breathe () class terrestrial (Animal): def __init__ (Self,name,breathep=lungbreathe ()): Super (terrestrial,self). __init__ (Name,breathep) class aquatic (Animal): def __init__ (self,name,breathep= Cheekbreathe ()): super (Aquatic,self). __init__ (Name,breathep) c = Terrestrial (' tabby cat ') c.performbreathe () fish = Aquatic (' Mermaid ') fish.performbreathe ()
We find that the second way is to implement the class of a behavior in the form of an interface, and you might think that the second method has a significantly longer code implementation and why I use the second way.
In today's example, there is no doubt that the first method is simple, so let's take a look at the purpose of the single duty principle:
- Can reduce the complexity of classes, a class is responsible for only one responsibility, its logic is certainly more than the responsibility of more than the responsibility of more simple
- Improve the readability of the class and improve the maintainability of the program
- The risk of change is reduced, the change is inevitable, if a single responsibility principle is well adhered to, when a function is modified, it can significantly reduce the impact on other functions
- The single principle of responsibility also applies to modular programming
Good! So the core of a single responsibility is nothing more than reducing complexity and improving readability and extensibility, and if you refine and refine the kind of animal you want, follow the first method--do you want to rewrite multiple breathing methods in a derived class? But the animal breath way is nothing more than this, so the second method is more suitable for complex occasions, so each method is different from the scene, own grasp!
You are welcome to ask questions and questions about my blog content! Thanks
Author: Pat Province Mr.
Single duty principle--Design pattern series