Open pit reason
Recently I touched some front end and found that many people do not understand design patterns (me neither). I hereby welcome you to learn design patterns from the front end. I hope this series can help you to write excellent code in your work.
What I hope this series can achieve
- Familiar with common design patterns and front-end implementations
- Understand what problems each design pattern solves and what the pros and cons are
What are design patterns
Design patterns represent best practices and are generally adopted by experienced object-oriented software developers. Design pattern is a solution to the common problems faced by software developers during software development. These solutions have been developed by numerous software developers over a long period of trial and error.
A design pattern is a set of repeated, well-known, catalogued code design lessons. Design patterns are used to reuse code, make it easier for others to understand, and ensure code reliability. There is no doubt that design patterns are a win-win for yourself, others and systems. Design patterns make coding truly engineering. Design patterns are the cornerstones of software engineering, like the bricks and stones of a mansion. The rational application of design patterns in projects can perfectly solve many problems. Each pattern has its corresponding principle in reality, and each pattern describes a recurring problem around us, as well as the core solution of the problem, which is also the reason why design patterns can be widely used
The above paragraph is my online copy, plus personal understanding is
- Design pattern is the repeated practice and best practice (essence) summed up by predecessors to solve a class of problems.
- Using design patterns makes code easy to reuse, easy to understand, and reliable
- Advanced advanced code farmers need to master the knowledge and skills
- For the interview
There are three categories and six principles of design patterns
There are three major categories of design patterns
According to the reference book on design patterns, Design Patterns – Reusable Object-oriented Software Elements, there are 23 general design patterns. These patterns fall into three broad categories
Creation pattern
- Factory Pattern
- Abstract Factory Pattern
- Singleton Pattern
- Builder Pattern
- Prototype Pattern
Structural mode
- Adapter Pattern
- Bridge Pattern
- Composite Pattern
- Decorator Pattern
- Facade Pattern
- Flyweight Pattern
- Proxy Pattern
Behavioral pattern
- Chain of Responsibility Pattern
- Command Pattern
- Interpreter Pattern
- Iterator Pattern
- Mediator Pattern
- Memento Pattern
- Observer Pattern
- State Pattern
- Null Object Pattern
- Strategy Pattern
- Template Pattern
- Visitor Pattern
The relationship between these design patterns is as follows
Six principles of design patterns
1. Open Close Principle
The open closed principle means: open for extensions, closed for modifications. When the program needs to be extended, you cannot modify the original code to achieve a hot plug effect. In short, to make the program extensible, easy to maintain and upgrade. To achieve this, we need to use interfaces and abstract classes, which will be discussed later in the concrete design.
2. Liskov Substitution Principle
Richter’s substitution principle is one of the basic principles of object-oriented design. Richter’s rule of substitution says that wherever a base class can appear, a subclass must appear. LSP is the cornerstone of inheritance reuse. The base class can be reused only when the derived class can replace the base class and the function of the software unit is not affected. The derived class can also add new behaviors on the base class. Richter’s substitution principle is a complement to the open – close principle. Abstract is the key step to realize the open and close principle, and the inheritance relationship between base class and subclass is the concrete realization of abstraction, so the Richter substitution principle is the specification of the concrete steps to realize abstraction.
3. Dependence Inversion Principle
This principle is the basis of the open closed principle, the concrete content: programming for interfaces that rely on abstraction rather than on concrete.
4. Interface Segregation Principle
This principle means that it is better to use multiple isolated interfaces than a single one. It also has another meaning: reducing coupling between classes. Thus, in fact, design mode is a software design idea that is easy to upgrade and maintain based on large-scale software architecture, and it emphasizes reducing dependence and coupling.
5. Demeter Principle
The least known principle means that an entity should interact with other entities as little as possible, so that the functional modules of the system are relatively independent.
2. Composite Reuse Principle
The principle of composite reuse is to use composition/aggregation rather than inheritance whenever possible.
conclusion
This section is a simple introduction to design patterns, as well as the three categories and six principles of design patterns. Stay tuned for a design pattern and its implementation in each of the following sections.