Programming is a process of thinking before coding , Thinking is better than coding skills , In the process of thinking, we will consider the reusability of the code , reliability , Easier to understand , At this point, design patterns are used to make code writing engineering , This article sorts out the six principles of design patterns .

Single responsibility principle

Single responsibility principle (Single Responsibility Principle)

There should never be more than one reason for a class to
change.( There is one and only one reason for a class change )

No matter what I'm asked to do , I only do one thing , You asked me to go downstairs to get the express , Take the garbage down by the way , I'm sorry , I don't do it , I only take express delivery .

advantage :

Class complexity reduction , There is a clear definition of what responsibilities to achieve ;

Readability improved , Reduced complexity , Of course, the readability has improved ;

Improved maintainability , Readability improved , That would be easier to maintain ;


Risk reduction caused by change , Change is essential , If the single responsibility of the interface is well done , An interface modification only affects the corresponding implementation class , No effect on other interfaces , This is the scalability of the system , Maintainability is very helpful .

Interface isolation principle

Interface isolation principle (Interface Segregation Principle)

1,Clients should not be forced to depend upon interfaces that they don't
use.( The client should not rely on interfaces it does not need )

2,The dependency of one class to another one should depend on the smallest
possible interface.( Dependencies between classes should be based on the smallest interface )

for instance , class A adopt Interface1 Dependency class B, method 1, method 2, method 3; class B adopt Interface1
rely on D, method 1, method 4, method 5, Take a look at what has changed between not using the interface isolation principle and using the interface isolation principle .

The principle of Dependence Inversion

The principle of Dependence Inversion (Dependence Inversion Principle)

1,High level modules should not depend upon low level modules.Both should
depend upon abstractions. ( High level modules should not rely on lower level modules , Both should rely on their abstractions )

2,Abstractions should not depend upon details. ( Abstraction should not depend on details )

3,Details should depend upon abstractions. ( Details should rely on abstraction )

for instance , class A Direct dependence class B, If you want to class A Change to dependency class C, Must be modified class A Code to achieve . In this scenario , class A It is usually a high-level module , Responsible for complex business logic ; class B
And class C It's a low level module , Responsible for basic atomic operations ; If the class is modified A, It will bring unnecessary risk to the procedure .

Solution : take class A Modify to dependency Interface Interface1, class B and class C They are implemented separately Interface Interface1, class A adopt Interface Interface1 Indirect and
class B perhaps class C Make a connection , It will greatly reduce the modification class A The probability of .

Ritz's substitution principle

Ritz's substitution principle (Liskov Substitution Principle)

1,If for each object o1 of type S there is an object o2 of type T such that
for all programs P defined in terms of T,the behavior of P is unchanged when o1
is substituted for o2 then S is a subtype of T.( If for each type is S Object of o1, All of them are of type T Object of
o2, So that T All defined procedures P In all objects o1 Replace them with o2 Time , program P There was no change in behavior , So the type S It's the type T Subtypes of )

2,Functions that use pointers or references to base classes must be able to
use objects of derived classes without knowing it.( All references to a base class must be able to use objects of its subclass transparently )

for instance , There is a function P1, from class A complete , Now we need to change the function P1 Expand , The expanded function is P, among P From the original function P1 And new features P2 form , new function P from
class A Of Subclass B To complete , be Subclass B In the completion of new functions P2 At the same time , It may lead to the original function P1 Failure occurred .

Solution : When using inheritance , Follow the principle of Richter substitution . class B inherit class A Time , In addition to adding new methods to complete the new functions P2 External , Try not to rewrite Superclass A
Methods , Try not to overload the parent class A Methods .

Inheritance has such a meaning : Any method that has been implemented in the parent class , In fact, it is setting a series of norms and contracts , Although it does not require all subclasses to comply with these contracts , However, if the subclass makes any changes to these methods , It will destroy the whole inheritance system , The principle of Richter's substitution expresses this meaning .

Inheritance is one of the three characteristics of object-oriented , At the same time, it brings great convenience to program design , It also brings disadvantages . Using inheritance, for example, can be intrusive , Program portability is reduced , The coupling between objects is increased , If a class is inherited by another class , When the class needs to be modified , All subclasses must be considered , And after the parent class is modified , All functions involving subclasses are likely to fail .

advantage :

Code sharing , Reduce the workload of creating classes , Each subclass has the methods and properties of the parent class ;

Improve code reusability , Scalability .

Improve the openness of products or projects .

Dimitar's law

Dimitar's law (Law of Demeter)

1,Each unit should have only limited knowledge about other units: only units
"closely" related to the current unit.( Each unit can only have limited knowledge of other units : It's just a unit that is closely related to the current unit )

2,Each unit should only talk to its friends; don't talk to
strangers.( Each unit can only talk to its friends : Can't talk to unfamiliar units )

3,Only talk to your immediate friends.( Only talk to your direct friends )

for instance , We passed mobile phone read Wechat reading APP Internal book , How to design class writing ?

Mobile phone and Books , These two cannot be called directly , need Mobile phone and Wechat reading APP Class calls first , Then read on wechat APP class Reconciling book
Class can be called , So that we can follow the Dimitar rule .

Open close principle

Open close principle (Open Closed Principle)

Software entities (classes, modules, functions, etc.) should be open for
extension, but closed for modification.( Objects in software ( class , modular , Functions and so on ) It should be open to expansion , But it's closed to changes )

Within the software life cycle , Because of the change , When the original code of software needs to be modified due to upgrade and maintenance , Errors may be introduced into old code , It may also make us have to refactor the whole function , And the original code needs to be retested .

Solution : When software needs to change , Try to achieve change by extending the behavior of software entities , Instead of changing existing code .


The principle of single responsibility tells us that the implementation class should have a single responsibility ;

The principle of interface isolation tells us to simplify and simplify the interface design ;

The principle of dependency inversion tells us to program interface oriented ;

The principle of substitution tells us not to destroy the inheritance system ;

Dimitar's law tells us to reduce coupling ;

The principle of opening and closing is the general principle , Tell us to be open to expansion , Close for changes ;

©2019-2020 Toolsou All rights reserved,
airflow Question series 2 —— task keep running Suspended animation PowerShell Use in WebClient Download the file and get the download progress ToastUtils Use of PHP Printout log( Essence )2020 year 6 month 26 day C# Class library Enum( Extension method )el-ui:select Get value element ui Drop down box search function ( Essence )2020 year 6 month 26 day C# Class library Exception handling help class uboot set up nfs parameter ——linux kernel use nfs Mode to load the root file system rootfs( Six )keras Data generator -- Data enhancement