Home > Web Front-end > Front-end Q&A > What is design pattern in javascript

What is design pattern in javascript

青灯夜游
Release: 2021-10-20 16:11:28
Original
1496 people have browsed it

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.

What is design pattern in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

What are design patterns?

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

Design principle

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

Fifteen common design patterns in JavaScript

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template