[design mode] overview

lisin-lee-cooper 2022-05-22 11:39:05 阅读数:178


One . summary

1.1 Concept
Software design pattern (Software Design Pattern), Also called design pattern , It's a set that's been used over and over again 、 Most people know that 、 Catalogued 、 Summary of code design experience . It describes some recurring problems in the software design process , And the solution to this problem . in other words , It's a series of solutions to specific problems , It is the summary of the code design experience of the predecessors , It has certain universality , It can be used repeatedly .

1.2 classification

Create pattern :
Used to describe “ How to create an object ”, Its main feature is “ Separate the creation and use of objects ”.GoF( The gang of four ) A single example is provided in the book 、 Prototype 、 Factory method 、 Abstract factory 、 Builders, etc 5 Creation mode .

Structural mode :
Used to describe how a class or object is organized into a larger structure according to a certain layout ,GoF( The gang of four ) There are agents in the book 、 Adapter 、 The bridge 、 decorate 、 appearance 、 Enjoying yuan 、 Combination etc. 7 A structural pattern .

Behavioral patterns :
It is used to describe how classes or objects cooperate with each other to complete tasks that cannot be completed by a single object , And how to assign responsibilities .GoF( The gang of four ) Template methods are provided in the book 、 Strategy 、 command 、 Responsibility chain 、 state 、 The observer 、 Intermediary 、 iterator 、 The visitor 、 Memorandum 、 Interpreters, etc. 11 Behavioral patterns .

Two .UML chart

drawing tools

2.1 Concept
Unified modeling language (Unified Modeling Language,UML) It's a visual modeling language for software design . It is characterized by simplicity 、 Unified 、 Graphical 、 It can express dynamic and static information in software design .
UML From different perspectives of the target system , Define the use case diagram 、 Class diagram 、 Object graph 、 State diagram 、 Activity diagrams 、 Sequence diagram 、 Collaboration map 、 Component diagram 、 Deployment diagram, etc 9 Seed map .

2.2 Class diagram :
Class diagram (Class diagram) It shows the static structure of the model , Especially the classes that exist in the model 、 The internal structure of classes and their relationships with other classes . Class diagrams don't show transient information . Class diagram is the main part of object-oriented modeling .

2.3 The function of class diagram
In software engineering , A class diagram is a static structure diagram , Describes the collection of classes of the system , The properties of a class and the relationships between classes , It can simplify people's understanding of the system ;
Class diagram is an important product of system analysis and design , It's an important model for system coding and testing .

2.4 The representation of class diagram

attribute / The plus and minus signs preceding the method name represent this property / Visibility of methods ,UML There are three symbols for visibility in class diagrams :

+: Express public
-: Express private
#: Express protected
The complete representation of attributes is : visibility name : type [ = The default value ]

The complete representation of a method is : visibility name ( parameter list ) [ : Return type ]

Be careful :

1, The content in brackets is optional
2, There are also ways to put types before variable names , The return value type precedes the method name

Example :

Person Class diagram
The figure above defines two methods

method() Method : The modifier is public, No parameters , no return value .
method1() Method : The modifier is private, Receive one int Parameters , The return value type is int.

2.5 Connections

Association is a kind of reference relationship between objects , Used to represent the relationship between one class of objects and another class of objects , Such as teachers and students 、 Master and apprentice 、 Husband and wife, etc . Association is the most commonly used relationship between classes , It can be divided into general relationship 、 Aggregation and composition .

1, One way Association
 One way Association

stay UML One way Association in the class diagram is represented by a solid line with an arrow . The picture above shows that every customer has an address , It's by making Person Class holds a type of House Member variable class implementation of .

2, Two way connection

 Insert picture description here

It's easy to see from the picture above , The so-called two-way association is that both sides hold each other's type of member variables .

stay UML In class diagram , Bidirectional association is represented by a straight line without an arrow . The picture above shows Customer Maintain one in the class List, It means that a customer can buy more than one product ; stay Product Maintain one in the class Customer The member variable of type indicates which customer purchased the product .

3, Self correlation
 Insert picture description here

Self correlation in UML The class diagram is represented by a line with an arrow pointing to itself . The picture above means Node Class contains the type Node Member variables of , That is to say “ Self contained ”.

Aggregate relationship

Aggregation is a kind of association , It's a strong correlation , It's the relationship between the whole and the part .
stay UML In class diagram , The aggregation relationship can be represented by a solid line with a hollow diamond , Rhombus pointing to the whole . The figure below shows the relationship between University and teachers :

 Insert picture description here

synthetic relation
Composition represents the relationship between the whole and the parts of a class , But it's a stronger aggregation .
stay UML In class diagram , A combination is represented by a solid line with a solid diamond , Rhombus pointing to the whole . The diagram below shows the relationship between head and mouth :

 Insert picture description here
Dependency relationship

Dependency is a kind of usage relationship , It's the weakest way of associating objects , It's a temporary connection . In the code , Methods of a class pass through local variables 、 Method or a call to a static method to access another class ( Dependent class ) Some of the ways to accomplish some of the responsibilities .

stay UML In class diagram , Dependencies are represented by dotted lines with arrows , The arrow points from the use class to the dependent class . The diagram below shows the relationship between driver and car , The driver drives the car :

 Insert picture description here

Inheritance relationships

Inheritance is the most coupling relationship between objects , To express a general or special relationship , Is the relationship between the parent class and the child class , It's an inheritance relationship . stay UML In class diagram , The generalization relationship is represented by a solid line with a hollow triangle arrow , The arrow points from the child class to the parent class . When the code is implemented , send
Using object-oriented inheritance mechanism to realize generalized relationship . for example ,Student Classes and Teacher Classes are Person Subclasses of classes , The class diagram is shown in the following figure :

 Insert picture description here
Realization relationship
Is the relationship between the interface and the implementation class . In this relationship , Class implements the interface , The operations in the class implement all the abstract operations declared in the interface . stay UML In class diagram , The implementation relationship is represented by a dotted line with a white triangle arrow , The arrow points from the implementation class to the interface . for example , Cars and boats make transportation possible .

 Insert picture description here

3、 ... and . Software design principles

3.1 Opening and closing principle
Open to expansion , Turn off for changes . When the program needs to be extended , You can't change the old code , Achieve a hot plug effect . in short , It's to make the program extensible , Easy to maintain and upgrade .

3.2 Richter substitution principle
Richter substitution principle : Any place where a base class can appear , Subclasses must appear . Easy to understand : A subclass can extend the functionality of a parent class , But you cannot change the original functionality of the parent class . let me put it another way , When a subclass inherits a parent class , In addition to adding new methods to complete the new functions , Try not to override the methods of the parent class .

3.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 . In short, it requires programming the abstraction , Do not program the implementation , This reduces the coupling between the customer and the implementation module .

3.4 Interface isolation principle
The client should not be forced to rely on methods it does not use ; The dependency of one class on another should be based on the smallest interface .

3.5 Dimitar's law
Dimiter's law is also called the principle of least knowledge . Just talk to your direct friends , Not with “ A stranger ” speak (Talk only to your immediate friends andnot to strangers).
Its meaning is : If two software entities do not need to communicate directly , Then there should be no direct mutual calls , The call can be forwarded through a third party . The goal is to reduce the coupling between classes , Improve the relative independence of modules . In Dimitar's law “ friend ” Refer to : The current object itself 、 Member object of current object 、 The object created by the current object 、 Method parameters of current object, etc , These objects are associated with the current object 、 Aggregate or combine relationships , Methods that can directly access these objects .

3.6 Synthetic multiplexing principle
Try to use association relations such as composition or aggregation to achieve , Second, consider using inheritance relationship to achieve .
In general, class reuse is divided into inheritance reuse and composition reuse .
Although inheritance reuse is simple and easy to implement , But it also has the following disadvantages :
Inheritance reuse destroys class encapsulation . Because inheritance exposes the implementation details of the parent class to the child class , The parent class is transparent to the child class , So this reuse is also called “ White box ” Reuse ;
The coupling degree between subclass and parent is high . Any change in the implementation of the parent class will result in a change in the implementation of the child class , This is not conducive to class expansion and maintenance ;
It limits the flexibility of reuse . The implementation inherited from the parent class is static , Defined at compile time , So it's impossible to change at run time . When combined or aggregated multiplexing is used , You can incorporate existing objects into new objects , Make it part of a new object , The new object can call the function of the existing object , It has the following advantages :
It maintains class encapsulation . Because the internal details of the component object are invisible to the new object , So this reuse is also called “ black box ” Reuse ;
Low coupling between objects . You can declare abstractions in a member of a class ;
High flexibility of reuse . This reuse can be done dynamically at run time , New objects can dynamically reference objects of the same type as component objects .

copyright:author[lisin-lee-cooper],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/142/202205211747004263.html