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

How to realize the Javascript Factory pattern

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to realize the Javascript factory model". In the daily operation, I believe many people have doubts about how to realize the Javascript factory model. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to realize the Javascript factory model". Next, please follow the editor to study!

Basic introduction

The simple factory model is the most basic one of the factory model. By defining a factory class, a specific product class is instantiated according to the parameters.

Give examples to illustrate

Let's give an example: suppose we develop a travel industry website that sells air tickets, hotels and other products. A user is going to buy a ticket. We can define the relevant classes as follows:

Var productEnums = {flight: "flight", hotel: "hotel"}; function Flight () {console.log ("This is Flight");} function Hotel () {console.log ("This is Hotel");} function User () {this.shopCart = [];} User.prototype = {constructor: User, order: function (productType) {var product = null Switch (productType) {case productEnums.flight: product = new Flight (); case productEnums.hotel: product = new Hotel (); default:} this.shopCart.push (product);}} var user = new User (); user.order (productEnums.flight)

This code defines three classes: user class User, air ticket class Flight, hotel class Hotel, in which User contains reservation method. Users can pass in the product type directly when booking. At first glance, this code looks fine, but the requirements and business change from time to time. If the company expands its business and increases its visa business, we have to modify the User class to ensure that it supports visas. Of course we can do this, but what's wrong with modifying the User class directly? is there a better way?

The first thing to say is the User class, which represents the user class, and the user class essentially has nothing to do with a specific type of business, that is, the business may increase at any time, but the user's code about the business is to create product orders. The new visa business is essentially no different from the existing air tickets and hotels. If you have to modify the User class for each additional business, it is very bad for the stability and maintainability of the code. A better solution is to have a special order creation class to manage different businesses, which is a simple factory.

We modify the code as follows:

Var productFactory = (function () {var productFactories = {"flight": function () {return new Flight ();}, "hotel": function () {return new Hotel ();}}; return {createProduct: function (productType) {return productFactories [productType] ();}) User.prototype = {constructor: User, order: function (productType) {this.shopCart.push (productFactory.createProduct (productType));}}

There are two main changes to this code:

(1) A product factory is added to return different objects according to different product types.

(2) modify the order method of the User class to call the create product method in the factory class.

The benefits of this are:

(1) make the order method of User more focused, only do the function of booking products, and extract and create product orders to special factory classes, the code is more concise and clear.

(2) A factory that specializes in managing product. It is easy to add new products and there is no need to modify the User class.

Summary description

The main feature of the simple factory pattern is the separation of the creation and use of objects, which is mainly composed of three parts:

1. Object usage class, which is a consumer created by the factory, regardless of the type of object and the creation process

two。 Factory class, which creates different objects according to the passed parameters and returns them to the object usage class, including the creation process of different objects. If there are different objects, modify the class.

3. Object classes, different categories produced by different businesses, are products produced by factories.

Advantages of simple factory model

1. The factory class centralizes the creation of all objects and facilitates the unified management of object creation.

two。 The users of the object only use the product and realize a single responsibility.

3. It is easy to expand, and if you add a new business, you only need to add the methods of producing business objects in the relevant business object class and factory class, and do not need to modify anything else.

Applicable scenario

1. Different instances need to be generated according to different parameters. These instances have some common scenarios.

two。 Users only need to use the product and do not need to know the creation details of the product.

Note: unless it is applicable to the scenario, the factory mode should not be abused, which can cause code complexity.

At this point, the study on "how to implement the Javascript factory model" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Internet Technology

Wechat

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

12
Report