Several common JavaScript design patterns

A design pattern is a proven solution to a specific problem that recurs in a specific environment in software development.

In the process of writing JS code, using certain design patterns can make our code more elegant and flexible.

 

Each design pattern consists of the following:

1. Schema name: A friendly name for the schema

2. Circumstances and problems: describe the circumstances under which specific problems arise

3. Solution: Describe how to solve the problem

4. Effect: describe the effect of applying the mode and the possible problems

 

Simply put, a pattern is some experience, a set of scenarios/problems + solutions.

Why learn design patterns?

There are many types of design patterns, and each pattern has its corresponding scene. It cannot be arbitrarily considered that a certain pattern is the optimal solution.

By learning these design patterns, you can find the feeling of "encapsulating changes", "loose coupling", and "programming against interfaces", so as to design programs that are easy to maintain, easy to reuse, have good scalability and sufficient flexibility.

By learning design patterns, you can understand the idea of ​​object-oriented programming (SOLID), and in the end, you can abandon design patterns.

Apply these ideas to your code and write code that is highly cohesive, low-coupling, extensible, and easy to maintain.

At this point, there is no design pattern in mind, and there are design patterns everywhere.

That's what learning design patterns is for.

 

Several common design patterns

singleton pattern

The reason why it is called the singleton pattern is because it is limited to a class, it only allows one instantiated object.

The classic way to do this is to create a class that contains a method that will create a new instance object if no object exists.

If the object exists, this method simply returns a reference to this object.

adapter mode

Convert the interface (method or property) of a class (object) into another interface that the client wants.

The adapter pattern enables classes (objects) that would otherwise not work together due to incompatible interfaces to work together.

Observer pattern

One or more observers are interested in the state of an observed, and register their interest in the observed by attaching themselves.

When the observer changes, and this is what the observer cares about, a notification will be generated, this notification will be sent, and finally the update method of each observer will be called.

When observers are not interested in the state of the observed, they simply strip themselves away.

proxy mode

Pass the access of one object to another object to operate.

为了保证当前对象的单一职责,而需要创建另一个对象来处理调用当前对象之前的一些逻辑以提高代码的效率,状态判断等。

模式并不是我们要追求的目标,我们的目标是写出有生气的代码,以有生气的代码为目标去写,你会发现有一些模式会自然而地出现在你的代码中。

这才是模式该用到的时候。

而且随 着你经验的增加,你会发现有一些新的模式自然产生,它与你用的语言,与你的所开发的应用,甚至与你的性格、信仰都有关系。

这时就你超越了大牛,拥有了你自己的模式语言,写出了具备无名特质的代码,踏上了属于你自己的编程永恒之道……

来源:千锋HTML5

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324731358&siteId=291194637