Design Pattern Understanding

Insert picture description here

To write excellent code, design patterns are the soul of code

Understanding of design patterns:
设计模式是针对软件设计中常见问题提出的典型解决方案;

设计模式可以用于解决代码中反复出现的设计问题;

设计模式不是一段特定的代码,而是解决特定问题的一般性概念;

设计模式和方法或库的使用方式不同,你很难在自己的程序中直接套用设计模式;

可以根据模式实现符合自己程序实际所需的解决方案;类似于日志门面和日志实现的关系;

算法更像是菜谱: 提供达成目标的明确步骤。
 而模式更像是蓝图: 你可以看到最终的结果和模式的功能, 但需要自己确定实现步骤。
The content of the pattern:

The description of the pattern usually includes the following parts:

Intent: Briefly describe the problem and solution;

Motivation: further explain the problem and explain how the model provides solutions;

Structure: display the components of each part of the model and the relationship between them;

Of course there are other parts;

History of the pattern:

(Reference material, read
by yourself ) Who invented the design pattern?

Design patterns are not obscure, complicated concepts-the opposite is true.

Patterns are typical solutions to common problems in object-oriented design.

The same solutions have been used repeatedly in various projects, so eventually someone named them and described them in detail. This is basically how the pattern was discovered.

The concept of mode was first proposed by Christopher Alexander in his book " Architectural Mode Language ". This book introduces the “language” of urban design, and the basic unit of such “language” is the pattern. The model may include descriptions of how high the windows should be, how many floors a building should have, and how much vegetation should be in a block.

Four authors, Erich Gamma, John Freesides, Ralph Johnson, and Richard Hulme, accepted the concept of patterns. In 1994, they published the book " Design Patterns: The Foundation of Reusable Object-Oriented Software ", which applied the concept of design patterns to the field of program development. The book provides 23 patterns to solve various problems in object-oriented programming, and soon became a bestseller. Because the title of the book was too long, people referred to it as the "Book of the Gang of Four (GoF)", and soon it was further simplified as the "Book of GoF".

Since then, people have discovered dozens of object-oriented patterns. "Pattern method" began to become popular in other fields of program development. Nowadays, outside the field of object-oriented design, people have also proposed many other patterns.

Why learn design patterns:

The design pattern is a toolbox for common problems in the software design process, and the tools are various solutions that have been proven in practice.

Design patterns can guide you how to use object-oriented design principles to solve various problems;

Design pattern is a jargon between programmers, a universal language that communicates more efficiently;

Classification of design patterns:

All modes can be classified according to his intention or purpose,

Mainly divided into

Creation mode: Provides a mechanism for creating objects, increasing the flexibility and reusability of existing codes;

Structural pattern: how to assemble objects and classes into larger structures. At the same time, the structure is flexible and efficient;

Behavior mode: responsible for efficient communication and delegation of responsibilities between objects;

Refer to this website: https://refactoringguru.cn/design-patterns/catalog

Principles followed by design patterns:
单一职责原则;
接口隔离原则;
依赖倒转原则;
里氏替换原则;
开闭原则;(ocp)
迪米特法则;
合成复用原则;
The purpose of the design pattern:
代码的复用性;(相同功能的代码,不用多次编写)
代码编写规范(可读性,便于他人阅读和理解);
可扩展;(方便拓展新的功能)
可靠性;(当我们增加新的功能,对原来的功能不会影响)
程序呈现高内聚低耦合的特性;

设计模式包含面向对象的精髓,“懂了设计模式,你就懂了面向对象分析和设计的精要”;

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43549291/article/details/113817443