Classification and six principles of design patterns Design patterns ---

Design Patterns

1. Classification of Design Patterns

Overall, the design mode is divided into three categories :

Creational patterns, were found in five :

  Factory method pattern, abstract factory mode, single-mode embodiment, the builder pattern, the prototype model.

The structural model, a total of seven kinds :

  Adapter model, decoration model, proxy mode, the appearance mode, bridge mode, combination mode, Flyweight.

Behavioral models, a total of eleven kinds :

  Policy Model, a template method model, the observer model, iterator pattern, the responsibility chain mode, command mode devices, memo mode state mode, the visitor pattern, intermediary model to explain the mode.

2. The six principles of design patterns

General principles: the principle of opening and closing

  That is the principle of opening and closing open for extension, but closed for modification . When the program needs to expand, not to modify the original code, but to expand the existing code to achieve the effect of a hot-swappable. So in one sentence: In order to make the program better scalability, ease of maintenance and upgrades. Want to achieve this effect, we need to use interfaces and abstract classes and so on.

1. Single Responsibility Principle

  Do not type more than one cause of change, which means that each class should implement a single responsibility , failing that, they should split the class.

2. Richter substitution principle

  Richter substitution principle (lsp) is one of the basic principles of object-oriented design. Richter said that the principle of replacing, where any base class can appear, sub-class will be able to appear . LSP is inherited cornerstone multiplexed only when the derived class can replace the base class, the software unit of functionality is not affected, the base class can really be reused, derived class can add new behavior on the basis of the base class on . Richter substitution principle is a supplement to the "opening and closing principle". Implement the principle of opening and closing is a key step in abstraction . The inheritance relationship with a base class is a subclass of the abstract concrete realization , so Richter substitution principle is the specification for concrete steps to achieve the abstract .

  Richter replaced in principle, sub-categories and try not to override the parent class method overloading . Because the parent on behalf of a defined structure, through this interface specifications to interact with the outside world, the subclass should not just destroy him.

3. Dependency Inversion principle

  This principle is the basis for opening and closing, the specific content: oriented programming interface, and rely on the abstract does not depend specifically . When used in a specific class when writing code, not, and upper interface to interact with the specific type of interaction with the specific class.

4. Interface segregation principle

  The meaning of this principle is: the absence of the method could be determined by the subclass must implement each interface , if otherwise, the interface will be split. A plurality of interface isolation, than using a single interface (s interfaces to a set of interface methods) is better.

5. Demeter (minimum know principle)

  A class dependent on their class knows the better, that is to say no matter how complex class is dependent, should be logic is encapsulated in the interior of the method , supplied to the outside through the public methods . So that when a change is dependent class, with minimal impact to the class.

6. Synthesis of multiplexing principles

  Principle is to first use of synthetic polymeric / manner, rather than using inheritance .

Guess you like

Origin www.cnblogs.com/yjxyy/p/11146088.html