In JavaScript, design patterns are solutions proposed to solve certain specific problems in software development and can also be understood as some ideas for solving problems. Design patterns can help us enhance the reusability, scalability, maintainability, and flexibility of code.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Design pattern (Design pattern) is some solutions proposed to solve certain specific problems in software development. It can also be understood as some ideas for solving problems. Design patterns can help us enhance the reusability, scalability, maintainability, and flexibility of code. Our ultimate goal in using design patterns is to achieve high cohesion and low coupling of code.
What are high cohesion and low coupling?
Take a real-life example, such as a company. Generally, each department performs its own duties without interfering with each other. When various departments need to communicate, they will be connected through dedicated persons in charge. The same is true in software. A functional module only focuses on one function, and it is best for a module to only implement one function. This is the so-called cohesion. The interaction between modules and systems is inevitable. However, we must try to reduce the situation where a single module cannot be used independently or cannot be transplanted due to interaction, as much as possible. A separate interface is provided for external operations. This is the so-called low coupling
Single Responsibility Principle (SRP)
An object or Methods only do one thing. If a method takes on too many responsibilities, it is more likely that the method will need to be rewritten as requirements change.
Objects or methods should be divided into smaller granularities
Least Knowledge Principle (LKP)
A software entity should relate to as little as possible Interactions between other entities
Interactions between objects should be minimized. If two objects do not need to communicate directly with each other, then the two objects should not have direct contact with each other and can be transferred to a third party for processing
Open-Closed Principle (OCP)
Software entities (classes, modules, functions), etc. should be extensible, but cannot be modified
When you need to change the function of a program or add new functions to the program, you can use it When adding code, try to avoid changing the source code of the program to prevent affecting the stability of the original system
1. Singleton Pattern
2, Strategy pattern
3, Agent pattern
4, Iterator pattern
5, Publish-Subscribe pattern
6. Command mode
7, Combination mode
8, Template method mode
9, Flyweight mode
10, Chain of responsibility mode
11. Mediator pattern
12. Decorator pattern
13. State pattern
14. Adapter pattern
15. Appearance pattern
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of What is design pattern in javascript. For more information, please follow other related articles on the PHP Chinese website!