The purpose of the design pattern :
- Code reusability ( namely : Code for the same function , Not many times )
- Readability ( namely : Programming normalization , Easy for other programmers to read and understand )
- Extensibility ( namely : When new features need to be added , Very convenient , It's called maintainable )
- reliability ( namely : When we add new features , It has no effect on the original function )
- Make the program present high cohesion , Low coupling characteristics
1、 Principle of single responsibility
For a class , That is, a class should only be responsible for one responsibility , Split the class . Such as class A Responsible for two different responsibilities : duty 1, duty 2. Responsibility 1 Requirements change and change A when , May cause responsibility 2 Perform error , So we need to put the class A The granularity of is broken down into A1,A2, You can also implement a single responsibility at the method level
matters needing attention :
- Reduce class complexity , A class is responsible for only one responsibility
- Improve the readability of the class , Maintainability
- Reduce the risk of change
- Usually , We should abide by the principle of single responsibility , Only logic is simple enough , To break the single responsibility principle at the code level ; There are only enough methods in the class , The principle of single responsibility can be maintained at the method level
2、 Interface isolation principle
The client should not rely on interfaces it does not need , namely The dependency of one class on another should be based on the smallest interface ( Eliminate useless methods ), Split the interface , Abstract the common parts of multiple interfaces into a parent interface , Subinterfaces have only their own methods , Implementation classes do not need to implement invalid methods .A For the class ,B Is the implementation class of the interface , call A Method time , Implementation class of interface B Is the method parameter ( be used B class ), Just say A Depending on this interface B class
3、 Dependence Inversion Principle
-
High level modules should not rely on low level modules , Both should rely on their abstractions
-
Abstraction should not depend on details , Details should depend on abstraction
-
The central idea of dependency inversion is interface oriented programming ( Interface specification rules , The details are left to the implementation class )
Three ways of dependency transmission
- Interface delivery ( Ordinary method parameters directly accept interfaces as parameters )
- Construct method passing ( The interface is passed to the class as a constructor parameter )
- setter Method ( Use setter Method to pass the interface inside the class )
4、 Richter's principle of substitution
The problem of inheritance :
- All the implemented methods in the parent class , It's actually setting norms and contracts , Although it doesn't force all subclasses to follow these contracts , But if a subclass modifies any of these implemented methods , It will damage the whole inheritance system
- Using inheritance can be intrusive , The portability of the program is reduced , Increase coupling between objects , If a class is inherited by another class , When this 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
An overview of the principles :
If for each type is T1 The object of o1, All of them are of type T2 The object of o2, So that T1 All programs defined P In all objects o1 All generations into o2 when , Program P There is no change in behavior , So the type T2 It's the type T1 In other words , All references to the base class must transparently use the object of its subclass
Unconsciously override the parent method , Make the program result incorrect , Solution : Create a more basic base class , Let the two classes get rid of the inheritance relationship
matters needing attention :
- When using inheritance , Follow the Richter principle of substitution , Try not to override the method of the parent class in the child class
- Inheritance actually enhances the coupling of the two classes , Where appropriate , You can aggregate , Combine , Rely on to solve problems
5、 Opening and closing principle (ocp)
- A software entity such as class , Modules and functions should be Open to expansion ( To the provider ), Yes Modify close ( For the user ). Building framework with abstraction , Extend details with implementation
- When the software needs to change , As far as possible Change is achieved by extending the behavior of software entities , instead of Change by modifying existing code
- There's still interface oriented programming
6、 Dimitar's law
- Dimitar's law (Demeter Principle) Also known as the least known principle , It's a class The less you know about the classes you depend on, the better . in other words , No matter how complex the dependent classes are , Try to encapsulate the logic inside the class . In addition to the external public Method , Don't divulge any information , There's a simpler definition of Dimiter's law : Communicate only with direct friends
- A direct friend : Each object is coupled to other objects , As long as there is a coupling relationship between two objects , We say that the relationship between the two objects is friendship . There are many ways of coupling , rely on , relation , Combine , Polymerization, etc. . among , We call it appear Member variables , Method parameter , The class in the return value of the method is a direct friend , And the class that appears in the local variable is not a direct friend . in other words , A strange class is best not to appear as a local variable inside the class
- More popular : That is, in the method of a class new The variable that comes out is a strange class
matters needing attention :
- The core of Dimiter's law is to reduce the coupling between classes
- But notice : Because each class reduces unnecessary dependencies , So Dimitri's law only requires that classes be reduced ( Between objects ) Coupling relationship , It's not a requirement to be completely independent
7、 Synthetic multiplexing principle
- The principle is to use composition as much as possible / Way of aggregation , Instead of using inheritance
8、 The core idea of design principle
- Find out what changes may be needed in the application , Separate them out , Don't mix with code that doesn't need to change
- Programming to an interface , Instead of programming for implementation
- For loose coupling design between interactive objects
Two 、UML Class diagram
1、 Basic introduction
- UML——Unified modeling language UML( Unified modeling language ), It is a language tool for software system analysis and design , It is used to help software developers think and record the results of ideas
- UML It is a set of rules of symbols , Just like mathematical symbols and chemical symbols , These symbols are used to describe the various elements in the software model and their relationships , Such as class 、 Interface 、 Realization 、 generalization 、 rely on 、 Combine 、 Polymerization, etc.
- They describe their thoughts to others , The key is thinking and organization
- UML Picture classification
- Use case diagram
- Static structure diagram : Class diagram 、 Object graph 、 Package diagram 、 Component diagram 、 Deployment diagram
- Dynamic behavior diagram : Interaction diagram ( Sequence diagram and collaboration diagram )、 State diagram 、 Activity diagrams
- UML Class diagram
- Used to describe classes in the system ( object ) Its composition and class ( object ) All kinds of static relations between
- Relationships between classes : rely on 、 generalization ( Inherit )、 Realization 、 relation 、 Aggregation and combination
2、 rely on
- As long as the other party is used in the class , Then there is a dependency between them , If there is no other party, you can't compile
- Symbol ---------->(dependency)
- A Class B class ,A rely on B, The arrow points to B
3、 generalization
- Generalization is actually inheritance
- Symbol ——————————▷(generalization)
- A Class inheritance B class ,A Inherit B, The arrow points to B
4、 Realization
- It's the interface implementation relationship
- Symbol ----------▷(implementation)
- A Class implementation B class ,A Realization B, The arrow points to B
5、 relation (association)
-
Association is actually the relationship between classes , A special case of dependency
-
Relevance is oriented : It's a two-way relationship ( A person has an ID card , A personal ID card ( stay Person Classes and IDCard Class to create each other's entity properties )) And a one-way relationship ( A person has an ID card ( Only in Person Class IDCard Attributes of a class )), Self correlation ( Attributes of this class exist in objects of this class ( A tree structure ))
-
Correlation has multiplicity : Such as '1'( There is only one ),'0...'( Express 0 One or more ),'0,1'( Express 0 A or a ),'n...m'( Express n To m Every one can ),'m...*'( At least m individual )
-
type
- One way Association : Symbol Person——————————>IDcard
- Two way connection : Symbol Person——————————IDcard
- Self correlation : Symbol TreeNode——————————>TreeNode
6、 polymerization
- An aggregate relationship is a relationship between the whole and the part , The whole and the part can be separated . The aggregation relationship is A special case of association , So he's connected Navigation and multiplicity
- Such as : A computer consists of a keyboard (keyboard)、 Monitor (monitor), Mouse, etc ; The components of a computer can be separated from the computer , After being stripped out, it can exist alone
- Symbol ——————————◇(aggregation, Hollow diamond )
- monitor——————————◇keyboard
7、 Combine
- synthetic relation : It's also the relationship between the whole and the part , But the whole cannot be separated from the part
- Such as : In the program we define the entity :Person And IDCard、Head( head ), that Head and Person It's a combination ,IDCard and Person It's polymerization ( It can also be defined as a combination )
- Symbol ——————————◆(composition, Solid diamond )
- Head——————————◆Person