Design Pattern Tutorial Series - Design Pattern Classification and Six Principles

Design Pattern Tutorial Series - Design Pattern Classification and Six Principles

1. Classification of design patterns

[Creative Patterns] There are five types in total : Factory Method Pattern, Abstract Factory Pattern, Singleton Pattern, Builder Pattern, and Prototype Pattern.

[Structural Mode] There are seven types in total : adapter mode, decorator mode, proxy mode, appearance mode, bridge mode, combination mode, and flyweight mode.

[Behavioral patterns] There are eleven types in total : strategy pattern, template method pattern, observer pattern, iterative sub pattern, chain of responsibility pattern, command pattern, memo pattern, state pattern, visitor pattern, mediator pattern, and interpreter pattern.

2. Six principles of design patterns

2.1 Open Close Principle

The open-closed principle means that it is open to expansion and closed to modification. When the program needs to be expanded, the original code cannot be modified to achieve a hot-swappable effect. So the summary in one sentence is: in order to make the program scalable and easy to maintain and upgrade. To achieve this effect, we need to use interfaces and abstract classes, which we will mention in the specific design later.

2.2 Liskov Substitution Principle

Liskov Substitution Principle LSP is one of the basic principles of object-oriented design. The Liskov Substitution Principle says that wherever a base class can appear, a subclass can definitely appear. LSP is the cornerstone of inheritance and reuse. Only when the derived class can replace the base class and the function of the software unit is not affected, the base class can be truly reused, and the derived class can also add new behaviors on the basis of the base class. . The Liskov substitution principle is a complement to the "open-closed" principle. The key step in realizing the "open-closed" principle is abstraction. The inheritance relationship between base classes and subclasses is the specific implementation of abstraction, so the Liskov substitution principle is a specification for the specific steps to achieve abstraction.

2.3 Dependence Inversion Principle

This is the basis of the opening and closing principle. The specific content: True interface programming relies on abstraction rather than concreteness.

2.4 Interface Segregation Principle

This principle means: It is better to use multiple isolated interfaces than to use a single interface. It also means reducing the coupling between classes. From here we can see that design patterns are actually a software design idea, starting from large-scale software architecture, for the convenience of upgrade and maintenance. So it appears many times above: reduce dependencies and reduce coupling.

2.5 Demeter Principle (Least Known Principle)

Why is it called the least known principle? That is to say: an entity should interact with other entities as little as possible so that the system functional modules are relatively independent.

2.6 Composite Reuse Principle

The principle is to use composition/aggregation as much as possible instead of inheritance.

Summarize

img

This article is published by OpenWrite, a blog that publishes multiple articles !

OpenAI opens ChatGPT to all users for free. Voice programmers tampered with ETC balances and embezzled more than 2.6 million yuan a year. Spring Boot 3.2.0 was officially released. Google employees criticized the big boss after leaving the company. He was deeply involved in the Flutter project and formulated HTML-related standards. Microsoft Copilot Web AI will be Officially launched on December 1st, supporting Chinese Microsoft's open source Terminal Chat Rust Web framework Rocket releases v0.5: supports asynchronous, SSE, WebSockets, etc. The father of Redis implements the Telegram Bot framework using pure C language code . If you are an open source project maintainer, encounter How far can you endure this kind of response? PHP 8.3 GA
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/5587102/blog/10149560