Design Patterns-Overview

Source: Internet
Author: User

1, why use? Design patterns can make your code more concise, more elegant, more efficient, and more scalable.

2, the principle of design mode: Open and close principle (to expand opening, to modify the closure) (abstraction is the key)

- Single Responsibility Principle: the class should have a single responsibility (there are no multiple reasons for the change of the Class), otherwise the class should be split. (not understood)

-- the Richter substitution principle: Where any base class can appear, subclasses must be able to appear. (LSP) (a method in which the subclass of the specification does not override or overload the base class as far as possible) (one of the fundamental principles of object-oriented design) (complementary to the open and closed principle) (concrete implementation of abstraction)

- Dependency reversal principle: it relies on abstraction and does not depend on specifics. (programming is the need to rely on a specific class, do not interact with the class, and turn to the upper interface of the class to interact with) (interface-oriented) (the basis of the closed-open principle)

-- interface Isolation principle: Each interface, for the class that implements the interface, there is no redundant method, otherwise you need to split the interface. (using multiple isolation interfaces is preferable to a collection interface)

-- know the principle at least: the less the class knows about the classes it relies on, the better. (Dimitri Law) (regardless of how complex the logic of a dependent class is, subclasses only need to be used by the exposed method of logic encapsulation) (this principle guarantees that the dependent class change is the least affected by inheriting his class) (communication with direct friends only) (Friend relationship: There is a coupling between classes [dependencies, associations, aggregates, combinations] (Direct friends: class that is a member variable, method parameter, method return value) (unfamiliar class does not appear in local variables)

-- synthetic multiplexing principle: synthesis/aggregation is better than inheritance.

3. Mode application Scenario

----Structural Patterns

-- Adapter mode (Adapter): Existing interfaces and defined interfaces do not match. (class adapter mode and object's adapter mode)

Implementation Description:

Class adaptation (Primitive Class A has a method Funca, interface facea need to implement Funca, FUNCB, implement an adaptation Class B inheritance and a Implementation interface Facea) (to resolve the number of methods within the interface and the original class method does not match)

Object adaptation (changing the relationship of A and b in a class adaptation from inheritance to B holding an instance object of a) (resolves the naming mismatch and number mismatch between the original and existing interface methods)

Interface Adaptation (pile problem)

For interfaces, two concepts are derived: Identify interfaces (interfaces that do not declare any methods) and piles (classes that implement an interface but implement a method body are empty).

Identity interface: Used to identify a class that conforms to certain specifications. (This is very useful for someone who makes rules, how to do it without complying with it, and to implement your own code, which will cause a fatal error to the program)

Pile: A set of methods declared in an interface, sometimes some methods may be ignored to execute, or the return value does not make any sense, you can define a pile, so that the implementation of specific delay to the specific subclass. (not understood)

-- Decorative mode (Decorator): Extends a class function. (Dynamic add-on feature)

Implementation description: The original Class A defines a method Funca, defines the interface Facea has method Funca, defines the adornment Class B holds an instance object of a, implements the interface Facea. (intended to extend to the original class)

- Proxy mode: A series of operations in place of the original object. (Object access Control) (hides an object's specific information)

Implementation description: The original Class A definition has method Funca, the definition interface Facea has method Funca, defines the adornment Class B holds an instance object of a, implements the interface Facea, the client program through the interface Facea to call a Funca. (a complex interface designed to encapsulate the original class)

-- appearance mode (facade): decoupling. (Computer classes and individual component classes, in order to prevent coupling between components, use computer classes to hold instances of individual components, avoiding the association between components)

Implementation description: The original Class A, B, C, defines the appearance Class D holds an instance object of a, B, C. (designed to decouple the relationships of a, B, c)

- Bridging Mode (bridge): Abstraction and instantiation decoupling.

Implementation Description: Define the interface Facea contains Funca, define the bridge Class A holds the interface Facea an object and implement Funca,funca will be based on a certain logical call Facea Funca, so that the implementation of the interface facea the specific logical implementation of the various classes and the interface of the call logic separation, The client program calls A's method Funca to invoke the appropriate processing logic.

-- Combined mode (Composite): Decoupling the internal structure of the client program and complex elements. (tree nodes and trees) (partial whole mode) (manipulate complex elements like simple elements)

Implementation description: The implementation of the Tree (Khan) (not understood thoroughly)

-- enjoy meta mode (Flyweight): Shared object already exists. (Shared pool) (connection pool) (not understood)

Implementation Description:

-------Simple Factory mode (extended)

--Simple creation: A factory class is responsible for creating instances of some classes of the same interface that are implemented. (Passing related type name strings to differentiate between created classes)

--Personalized creation: On a simple basis, the factory class implements a creation method for each class. (personalization) (multiple methods)

--Static personalization creation: On the basis of personalization, the method of the factory class is set to the static method (static) of the class. (Multiple static methods)

----Create a pattern

-- factory Approach Mode (Factory method): To solve the simple Factory mode in the expansion must modify the factory class, the violation of the open and closed principle. (Simple Factory mode class creation relies on factory Class) (Good Extensibility) (support for new products)

Implementation Description: Defines a factory class interface that creates a factory class for each product class.

-- abstract Factory: Extended Support for product clusters (new products are not supported, new product clusters are available)

Implementation pattern: Defines a factory class interface, multiple product abstraction interfaces. (Product cluster)

-- Singleton mode (Singleton): Provides unique objects (when too many objects are created) (decompression) (objects can only be unique)

Implementation pattern: Inner class generates private static object, provided externally through public static method (inner class implementation)

- Builder Mode (builder): Creates a complex object that separates the creation process from the specific creation of parts of the miscellaneous object.

Implementation mode: Defines a builder interface creator, a director of class directors holding builder Builder interface, which invokes the method in the concrete builder ConcreteBuilder class to construct a complex product object.

- -prototype mode (PROTOTYPE): Cloning of objects. (local method, more efficient than new) (Simplified object creation) (Shallow copy: array, reference object) (Deep copy: Primitive type and wrapper type, String) (cloning over the constructor permission of the Class) (conflict with singleton mode)

-- Implementation mode: Overrides the Clone method of the object class. (cloneable interfaces, arrays, referencing objects need to be cloned separately)

----Relationship Mode

-Parent class and child class

-- policy mode (strategy): Let the specific changes of the interchangeable algorithm not affect the calling client program. (The decision is in the client program) (the algorithm that the client decides to call specifically)

Implementation mode: An abstract algorithm interface, an optional abstract class that provides auxiliary functions, a group of algorithm classes that implement interfaces, inherit abstract classes, and a client program that requires a correlation algorithm.

-- Template Method Mode: (parent class refers to tone class method) (not understood)

Implementation pattern: An abstract class that provides the main method, a group of algorithm classes that inherit the abstract class, and a client program that needs to invoke the concrete method through a reference to the abstract class.

- between classes

-- Observer Mode (OBSERVER): Two types of interfaces are dependent. (One party affects the other) (one party monitors the other party)

Implementation mode: One interface a depends on another interface B, and the implementation of interface a affects the implementation of B (an abstract class and his subclasses), and Interface B's implementation needs to monitor the dynamics of a and react.

- -iterator mode (Iterator): The traversal of the aggregation.

Implementation pattern: An aggregation interface and an iterator interface, the implementation of an iterator interface holds a reference to the aggregation interface, thereby manipulating the collective aggregation type.

-- responsibility Chain mode (Chain of Responsibility): Multiple objects are referenced to each other. (Hide specific calls to the user program)

Implementation pattern: A Class B inherits from the abstract class A, implements the interface Funa,a holds the Funa reference, can go to reference B's instance object, the client program invokes the corresponding operation through the instance object of B.

-- Command mode: decoupling.

Implementation mode: Command interface F, the command class that implements the interface C,c a reference to the class R that holds the final completion command, and invokes Class I to hold a reference to the command interface.

-The state of the class

-- Memo Mode (Memento): Backs up the object state for recovery.

Implementation pattern: An original Class A can create a memo class B, and storage Class C holds an instance of B to store the memo.

- State mode (state): different states, different behavior.

Implementation pattern: The original Class A holds the instance of State Class B, and a corresponds to the specific method of B based on the property value of B.

-by Intermediate Class

-- Visitor Mode (Visitor): Solve the problem of changing operations with small and frequent changes in element classes (ask me) (Extensibility: receiving different visitors, expanding operations) (in line with a single responsibility) (external call Element interface Reference)

Implementation pattern: An element interface, a visitor interface, an element interface that receives the visitor interface for some related operations.

-- Mediator mode (mediator): Decoupling between work-related worker classes.

Implementation pattern: A mediator interface that implements the interface of the Mediator class, which holds N worker class objects and coordinates them to work, the abstract worker class holds the corresponding mediator interface reference. (External call Broker Interface Reference)

- -interpreter mode (interpreter): explains related expressions. (Good scalability) (efficiency, performance, maintainability) (not recommended for easy use) (single problem frequency is very large, can be considered)

Implementation patterns: Expression interfaces (non-terminator expression classes and Terminator expression classes), a contextual context class. (not understood)




















Design Patterns-Overview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.