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

Example Analysis of Factory pattern in web Front end

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the example analysis of the factory pattern in the front end of web, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

Factory mode (Factory Pattern)

The factory pattern defines an interface for creating objects, and the subclass determines which class to instantiate. This pattern delays the instantiation of a class to a subclass. Subclasses can override interface methods to specify their own object type when created.

Class Product {constructor (name) {this.name = name} init () {console.log ('init')} fun () {console.log (' fun')}} class Factory {create (name) {return new Product (name)}} / / uselet factory = new Factory () let p = factory.create ('p1') p.init () p.fun ()

Scene

If you don't want a subsystem to be strongly coupled to the larger object, but you want the runtime to choose from many subsystems, then the factory pattern is an ideal choice.

Simply encapsulate the new operation, and you should consider whether to use factory mode when you encounter new

Different instances need to be created depending on the specific environment, and these instances all have the same behavior. At this time, we can use the factory pattern to simplify the implementation process, and at the same time, we can also reduce the amount of code required for each object, which helps to eliminate the coupling between objects and provide greater flexibility.

Advantages

The process of creating an object can be complex, but we only need to care about the result.

The constructor is separated from the creator, which accords with the "opening and closing principle".

A caller wants to create an object by knowing its name.

High scalability, if you want to add a product, just extend a factory class.

Shortcoming

When adding new products, it is necessary to write new specific product classes, which increases the complexity of the system to a certain extent.

Considering the expansibility of the system, it is necessary to introduce the abstraction layer, which is defined in the client code, which increases the abstraction and difficulty of understanding of the system.

When don't use it?

When applied to the wrong type of problem, this pattern introduces a lot of unnecessary complexity to the application. Unless providing an interface for creating objects is a design goal of the library or framework we write, I would recommend using explicit constructors to avoid unnecessary overhead.

The fact that the object creation process is efficiently abstracted behind an interface also poses problems for unit tests that depend on how complex the process can be.

Thank you for reading this article carefully. I hope the article "sample Analysis of Factory patterns in web Front end" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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