Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common design patterns of JavaScript

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "what are the common design patterns of JavaScript", the content is simple and clear, and I hope it can help you solve your doubts. Let the editor lead you to study and learn what common design patterns of JavaScript are.

Design pattern

Design pattern is a reusable solution summed up in a long period of experience and mistakes.

Design patterns fall into three main categories:

Creative design pattern: focus on the creation of objects

Constructor constructor mode, Factory factory mode, Singleton singleton mode, builder generator mode

Structural design pattern: the combination of objects and the relationship between resume objects

Decorator decorator mode, Facede appearance mode, Flyweight meta-mode, Adapter adapter mode, Proxy proxy mode

Behavioral design patterns: simplify and improve communication between objects

Mediator intermediary mode, Observer observer mode

Common design patterns

1. Observer mode

A target object maintains a series of objects that depend on it, automatically notifying observers of any changes to the state. In the observer mode, the observer needs to subscribe to the target object directly. There is a certain dependence between the observer and the target object.

There are four important concepts.

1 > Target object (observed): maintain a group of observed patients and provide a way to manage the observer.

2 > Observer: provides an update interface for updating when you receive a notification

3 > specific target object: represents the specific target object

4 > specific observer: represents the specific observer

2. Publish and subscribe mode

The publish and subscribe model can be said to be a variant of the observation pattern, an implementation. It uses a topic / event channel between publishers and subscribers, avoiding dependencies between publishers and subscribers.

3. Factory mode

The factory function provides a general interface to create objects, we can specify the common object types we want, we tell the factory function what type of objects we need and provide the corresponding data, and return the corresponding instance.

When to use factory mode

1 > when our object is more complex

2 > when we need to create different object instances according to different situations

3 > when we need to create many similar objects

Disadvantages: improper use will increase the complexity of the program

4. Abstract factory pattern

Abstract the factory pattern to extract the implementation details of the object. Suitable for scenarios that need to work with multiple objects.

5. Singleton mode

The idea of monolithic mode is to ensure that there is only one instance of a particular class, which means that the second time you create a new object using the same class, you should get exactly the same as the first time. (restrict a class to only one instantiated object.)

6. Intermediary model

Intermediary mode is to provide a central point for communication between different components of the system, reducing the degree of coupling between system components.

/ / implementation is similar to publish / subscribe model

The Observer pattern and the publish and subscribe pattern focus on maintaining the relationship between the target object and the observer, notifying all observers interested in changing the topic when the topic object changes. In intermediary mode, communications focused on restricted objects must communicate through intermediaries. Both advocate coupling.

7. Decorator mode

Decorator mode, through a decoration class to add behavior to the existing dynamic, as well as to decorate the original behavior.

8. Adapter mode

Use a new interface to wrap the existing interface to deal with the mismatch between the data and the interface

The above is all the content of the article "what are the common design patterns of JavaScript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report