Javascript all in one pattern

23 design patterns are divided into 3 categories:

In addition to these 23 classic design patterns, there are other new design patterns.

Concurrency mode
thread pool mode
Specification mode
Object pool mode
Employe mode
blackboard mode
empty object mode

Six principles of design patterns:

0, the general principle — opening and closing principle

Open open and closed;
When the program needs to expand, the original code cannot be modified, but to extend the original code, implement a hot-swappable effect;
So a sentence is summarized: in order to make the program’s scalability, easy maintenance and upgrade;

If you want to achieve this, we need to use interfaces and abstractions.

1, single responsibility principle

Don’t exist more than one reason why the class changes, that is, each class should achieve a single responsibility, otherwise you should split the class;

2, Lisk Substitution PrinciPle

Where the base class can appear, the subclass can appear;
Rivis replacement principle is to inherit the cornerstone, only when the derivative class can replace the base class, the function of the software unit is not affected, and the base class can be truly reused, and derived class It is also possible to add new behaviors on the basis of the base class;

Читайте также:  Unit test kotlin android

The principle of replacement is the supplement to the principle of «opening — closed»;
The key steps to implement the «opening and closing» principle are abstraction;
The inheritance relationship of the base class and subclass is the specific implementation of abstraction, so the replacement principle is a specific step in achieving abstraction;
In the Reverse Replacement Principle, the child is trying to rewrite and overload the method of the parent class;
Because the parent class represents the defined structure, the subclass should not be destroyed by the external interaction with the external interface through this specification;

3. Dependency Inversion PrinciPle

Interface programming, depending on abstraction, not dependent on specific;
When writing code, it is not interacting with the specific class, and interact with the specific class;

4. Interface Isolation Principle (Interface SegRegation Princi)

There is no method that there is no subclass without subclasses in each interface. If otherwise, you will split the interface;
Using multiple isolated interfaces, better than using a single interface (multiple interface methods to one interface);

5, Dimit Block / Leverage Demeter PrinciPle

The smaller the class knows the class, the better the class is known;
Whether it is complicated by the dependencies, the logic should be encapsulated inside the method, and it is provided to the outside by the public method;
This class can be minimized when the dependent class changes;

Another way to know the principle is: only communicate with direct friends;
As long as there is a coupling relationship, it is called a friend relationship;
Coupling is divided into dependence, association, polymerization, combination, etc.
We have called a member variable, method parameters, and the class in the method returned as direct friends;
Local variable, temporary variable is not a direct friend;
We require unfamiliar classes to appear as partial variables in classes;

6. Synthetic Reuse PrinciPle

Try to first use synthetic / polymeric methods, not inheritance;

Источник

Оцените статью