What are design patterns
In development, we encounter certain scenarios where a simple and elegant solution to a particular problem is a design pattern. A good design pattern can be reused in such scenarios. These things are particularly important for our front-end development.
Five principles of design patterns
- Single Responsibility Principle SRP: Single responsibility
- OCP: Can be extended, but not modified
- The Principle of Richter’s replacement is LSP: Do not destroy inheritance system
- The principle of Interface Independence (ISP) : Design a single and simple interface
- The dependency inversion principle is called DIP: Program interface oriented, not implementation oriented
Four characteristics to look out for in design patterns
- legibility
- Can expand sex
- reusability
- reliability
Semantics is very clear, here is no more explanation, here I want to talk about legibility, when we look at a lot of underlying source code, in fact, some of them are not very legibility, sometimes for the sake of speed at the expense of some readability.
The singleton pattern
The idea behind the singleton pattern is that a class can return a reference to an object (always the same) and a method to get that instance (it must be static, usually using the name getInstance). When we call this method, we return the reference held by the class if it is not empty, and create an instance of the class if the reference held by the class is empty and assign the instance’s reference to the reference held by the class. We also define the [constructor] of the class as a private method, so that code elsewhere cannot instantiate objects of the class by calling the constructor, only by using static methods provided by the class to get a unique instance of the class.
The simple way to think about it is if we have a popover, we’re going to use it a couple of times, but everything in it stays the same. We don’t need to create an instance every time we call it.
We can cache it the first time we use it, and then the second time we use it, we don’t have to recreate it, we just use it the first time we call it.
The singleton pattern segmentfault.com/a/119000001…
The strategy pattern
Usage scenarios: such as form validation, we need a lot of judgment, if use the if the else to write will be very difficult to read, if can also want before moving to write new code, the term is high coupling, decoupling, we can use the strategy pattern for the input and the content of the need to the algorithm of space, and not to modify the code before.
The strategy patternRefactoringguru. Cn/design – patt…
The proxy pattern
The proxy mode provides a substitute or placeholder for an object that can be preprocessed and rendered after the ontology is loaded.
For example, when we jump to a page, the rendering may be slow, so we can use load to occupy the space during rendering, so that users will not be anxious. After loading, the load disappears and the page displays. Here, too, there is a single responsibility.