Overview of design patterns

Source: Internet
Author: User

The concept of design patterns was first proposed by an architect. He tried to use a structured and reusable Method on drawings to obtain the basic elements of a building, this allows you to focus more on the design of specific buildings and towns. Gradually, this idea has become popular in the software field and has gained development.

Design pattern is a set of summary of code Design experiences that are repeatedly used, known to most people, classified and catalogued. The design pattern is used to make code reusable, make it easier for others to understand, and ensure code reliability.

Design Patterns are abstract content in the computer software field. The design patterns bring us the freedom from complicated and messy code, it makes the software system more stable, easier to expand, and easier to maintain.

Why design patterns?

The application of design patterns has no harm to individuals, enterprises, and society. First, it can improve personal programming, thinking, and design capabilities, greatly improve work efficiency, and allow individuals to gain greater development space. Second, for enterprises, the application design model can ensure the quality of software products, greatly improve system stability, and create more room for enterprises to increase profits. Finally, individuals and enterprises are constantly growing, it will naturally create more social values and make more contributions to society. Therefore, we need design patterns, enterprises need design patterns, and society also need design patterns. The design model may be an important factor that changes the fate of a person in the workplace, the key to the success or failure of an enterprise, or even the socio-economic development.

Basic Principles of Software Design

1. Single Responsibility Principle (SRP)
For a single responsibility principle, there should be only one reason for its change in a category. If a class has too many responsibilities, it is equivalent to coupling these responsibilities. A change in responsibilities may weaken or restrain the class from fulfilling other responsibilities. This coupling will lead to a fragile design. When a change occurs, the design will suffer unexpected damages. What software design really needs to do is to discover responsibilities and separate them from each other.

2. The Open-Closed Principle (OCP)
The open-closed principle means that software entities (classes, modules, functions, and so on) can be expanded but cannot be modified. That is, the extension is open and the change is closed. It is impossible for us to be an unknown crowdsourced Security Testing. We should try to make a class good enough during design, so we should not modify it if it is well designed. If it cannot be completely closed, when it changes, we will create an abstraction to isolate future similar changes.

3. Dependence Inversion Principle)
The dependency inversion principle means that the high-level module should not depend on the lower-level module, and both should depend on abstraction. abstraction should not depend on details, and details should depend on abstraction. To put it bluntly, it is necessary to program the interface rather than implement programming. For example, in computer hardware, if the memory is broken, you only need to change the memory, instead of changing the motherboard. Here the memory is an interface class, you only need to comply with the specifications, either.

4. Liskov Substitution Principle (LSP)
The Rys replacement principle, child types must be able to replace their parent type. In the software, replace all the parent classes with their child classes, and the behavior of the program will not change. It is precisely because the child type can be replaced that the module with the parent type can be expanded without modification.

5. Law of Demeter)
Dimit's Law: If two classes do not need to communicate with each other directly, they should not interact directly. If one class needs to call a method of another class, it can be forwarded by a third party. The weaker the coupling between classes, the more conducive it is to reuse. A class with weak coupling is modified and will not affect related classes. It mainly emphasizes the loose coupling between classes.

6. Composition/Aggregation Principle (CARP)
The principle of merging and aggregation reuse should be used as much as possible, and class inheritance should not be used as much as possible. Merging and aggregation is the relationship of "has a", while inheritance is the relationship of "is. Because inheritance is a strongly coupled structure, the Child class will change as the parent class changes. Therefore, it is not an "is a" relationship. We generally do not use inheritance. The principle of merging aggregation reuse is preferred, which helps maintain the encapsulation of each class and reduce the hierarchy of inheritance.

Classification of design patterns

According to the range, the design mode can be divided into class mode and object mode.

The class mode is used to process the relationship between the class and the subclass. These relationships are established through several folds, which are static and determined at the Compilation Time;

The object mode is used to process the relationships between objects. These relationships can change and be more dynamic at runtime.

According to the purpose, the design patterns can be divided into creation patterns, structural patterns, and behavior patterns.

The creation mode is used to process the object creation process;

Including Factory Method)

Abstract Factory)




The structural mode is used to process the combination of classes or objects;

Including Adapter)




Appearance (Facade)



The behavior pattern is used to describe how classes or objects interact and how responsibilities are assigned.

Including the Chain of Responsibility)


Interpreter (Interpreter)

Iterator Pattern)


Memorandum (Memento)

Observer (Observer)



Template Method)

Visitor (Visitor)

The design model is a summary of previous practical experience, and is the crystallization of the ideas that have been refined. We are not really studying the design model. Our real purpose is to learn this idea, apply it in practice, and learn to sum up and summarize it so that our thinking can become active, instead of sticking to computer coding, it increases to a certain theoretical space. Let the theory guide the practice and enrich the theory. In this way, a virtuous circle will be formed, and our theoretical and practical abilities will be continuously improved to realize the maximum social value of the individual.

Related Article

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.