Front-end design pattern application | Youth training camp

Front-end design pattern concepts

What are design patterns

Solution models for common problems in software design

  • Summary of historical experience
  • Language-agnostic

design pattern background

image.png

Design pattern classification

23 design patterns

  • Creation type - how to create an object
  • Structural type - how to flexibly assemble objects into larger structures
  • Behavioral - responsible for efficient communication and division of responsibilities between objects

Design patterns in browsers

Singleton pattern

definition

Globally unique access object

Application scenarios

Caching, global state management, etc.

Implement request cache demo

image.png
image.png
image.png

publish-subscribe model

definition

A subscription mechanism that notifies subscribers when the subscribed object changes.

Application scenarios

From the decoupling between system architectures to some implementation patterns in business, such as email subscription, online subscription, etc., they are widely used.
image.png

Implement user online subscription

image.png
image.png

Design Patterns in JavaScript

Prototype pattern

definition

Copy existing objects to create new objects

Application scenarios

Basic patterns of object creation in JS

Create users in online subscriptions

image.png
image.png

code pattern

definition

The access method to the original object can be customized and controlled, and some additional processing can be done before and after the update.

Application scenarios

Monitoring, proxy tools, front-end framework implementation, etc.

Implement user status subscription

image.png
image.png
image.png

iterative mode

definition

Access data in a collection without exposing the data type

Application scenarios

There are multiple data types in the data structure, such as lists, trees, etc., which provide common operation interfaces.
image.png

Use for of to iterate over all components

image.png
image.png

Design patterns in front-end

Vue component implements counter

image.png

Proxy for DOM operations in front-end framework

image.png

Hooks before and after Dom update

image.png

Combination mode

definition

Can be used in combination with multiple objects, or a single object can be used independently

Application scenarios

DOM, front-end components, file directories, departments

Summarize

Design patterns are not a silver bullet

  • It is relatively easy to summarize abstract patterns, but it is very difficult to apply abstract patterns to scenes.
  • More possibilities brought by the multiple programming paradigms of modern programming languages
  • Really excellent open source projects learn design patterns and continue to practice them

Guess you like

Origin blog.csdn.net/Azbtt/article/details/132162175