Common design Patterns

Source: Internet
Author: User

Content

1. Introduction to Design Patterns

2. Design Pattern classification

3. Design mode 6 principles

1. Introduction to Design Patterns

Design mode (Patterns)

--the basis of reusable object-oriented software

Design patterns are a set of reusable, most known, categorized purposes, code design experience Summary. Design patterns are used in order to reuse code, make code easier for others to understand, and ensure code reliability. There is no doubt that design patterns in others in the system are multi-win, design patterns so that code is truly engineering, design patterns are the cornerstone of software engineering, like a block of bricks and tiles in the building. The rational use of design patterns in the project can solve many problems perfectly, each of which has corresponding principles in the present, each of which describes a recurring problem around us, and the core solution of the problem, which is why it can be widely used.

2. Design Pattern classification

The classic "Design pattern" a book summed up 23 design patterns, these 23 models can be classified as, create, structure and behavior of 3 categories.

2.1. Create pattern

As mentioned earlier, the division of socialization is becoming more and more fine, natural in the software design is also so, so object creation and the use of objects separated also become an inevitable trend. Because object creation consumes many of the resources of the system, the creation of objects is studied independently, so that creating objects efficiently is an issue to be explored in creating patterns. There are 6 specific creation patterns available for research, namely:

Simple Factory mode (Factory);

Factory method Mode (Factory);

Abstract Factory mode (Factory);

Creator mode (Builder);

Prototype mode (PROTOTYPE);

Singleton mode (Singleton).

Description: Strictly speaking, the simple factory model is not one of the 23 design patterns summed up by Gof.

2.2 Structural Mode

After solving the object creation problem, the composition of the object and the dependencies between the objects become the focus of the developer's attention, because how to design the structure, inheritance and dependency of the object will affect the maintainability of the subsequent program, the robustness of the code, the coupling and so on. The design of the object structure can easily reflect the level of the designer, there are 7 specific structural patterns to be studied, respectively:

Appearance mode (facade);

Adapter mode (Adapter);

Agent mode (proxy);

Decoration mode (Decorator);

Bridge mode (bridges);

Combination mode (Composite);

Enjoy meta mode (Flyweight).

2.3 Behavioral Mode

After the object's structure and object creation have been solved, the behavior of the object is left behind, and if the object behaves well, the behavior of the object will be clearer and the collaboration efficiency will improve, there are 11 specific behavioral patterns to be studied, namely:

Template method mode;

Observer pattern (Observer);

Status mode (state);

Strategy mode (strategy);

Responsibility chain mode (Chain of Responsibility);

Command mode;

Visitor mode (Visitor);

Mediator mode (mediator);

Memo mode (Memento);

Iterator mode (Iterator);

Interpreter mode (interpreter).

3. Six principles of design patterns

3.1. Opening and closing principle (Open Close Principle)

The open and closed principle is to say to the expansion opening, to modify the closure . When the program needs to expand, can not modify the original code, to achieve a hot plug effect. So a nutshell is: In order to make the program good extensibility, easy to maintain and upgrade. To achieve this, we need to use interfaces and abstract classes, which we will refer to later in the specific design.

3.2. The principle of substitution on the Richter scale (Liskov Substitution Principle)

One of the fundamental principles of object-oriented design of the Richter substitution principle (Liskov Substitution Principle LSP). The Richter substitution principle says that where any base class can appear, subclasses must be able to appear. LSP is the cornerstone of inheritance reuse, only if the derived class can replace the base class, the function of the Software unit is not affected, the base class can be really reused, and the derived class can also add new behavior on the basis of the base class. The principle of substitution on the Richter scale is a supplement to the principle of "open-closed". The key step in implementing the "open-close" principle is abstraction. The inheritance of the base class and subclass is the concrete implementation of abstraction, so the principle of the substitution of the Richter scale is the specification of the concrete steps to realize the abstraction. --from Baidu Encyclopedia

3.3. Dependence reversal principle (dependence inversion Principle)

This is the basis of the open and close principle, the specific content: TRUE interface programming, relying on abstraction and not dependent on the specific.

3.4. Interface Isolation principle (Interface segregation Principle)

This principle means that using multiple isolated interfaces is better than using a single interface. or a reduction of the coupling between the class meaning, from here we see, in fact, the design pattern is a software design ideas, from the large software architecture, in order to upgrade and maintenance convenience. So there are multiple occurrences: reducing dependency and reducing coupling.

3.5, Dimitri Law (least known principle) (Demeter Principle)

Why is it called the least known principle, that is to say: an entity should be as small as possible interaction with other entities, so that the system function module is relatively independent.

3.6. Synthetic multiplexing principles (Composite reuse Principle)

The principle is to use composition/aggregation as much as possible, rather than using inheritance.

Factory mode

Factory mode (Factory pattern) is one of the most common design patterns in Java. This type of design pattern belongs to the Create pattern, which provides an optimal way to create an object.

In Factory mode, we create the object without exposing the creation logic to the client, and by using a common interface to point to the newly created object.


Intentions:

Defines an interface for creating objects, letting subclasses decide which class to instantiate. The Factory Method defers the instantiation of a class to its subclasses.

Applicability:

When a class does not know the class of the object it must create.

When a class wants to specify the object it creates by its subclasses.

When a class delegates the responsibility of creating an object to one of several helper subclasses, and you want to set which helper subclass is the agent this information is localized.

Simple Factory mode

Common design Patterns

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.