JS design mode--Detailed Factory mode

Source: Internet
Author: User

Factory mode is another creation pattern that focuses on object creation concepts. Its domain differs from other patterns in that it does not explicitly require us to use a constructor. Instead, a factory can provide a common interface for creating objects, where we can specify the type of factory objects we want to create.

    • Simple Factory mode: Use a class (usually a monomer) to generate an instance
    • Complex Factory mode: Use subclasses to determine which instance of a variable member should be the specific class.
Simple Factory mode
varBicycleshop =function () {}; Bicycleshop.prototype={sellbicycle:function (model) {varbicycle; Switch(model) { Case "A"://a type of bicycleBicycle =NewA ();  Break;  Case "B": Bicycle=NewB ();  Break;  Case "C": Bicycle=NewC ();  Break; } interface.ensureimplements (bicycle, bicycle); returnbicycle; }} 

This works well, but if you need to add some bike styles, such as I want to be able to produce a type D bike, this needs to change the switch portion of the bicycleshop, which is not very good.


Complex Factory mode

In order to compare with the simple factory, we use Bicycleshop as an example to illustrate the design concept of the factory model.

We are going to let each bike store decide which manufacturer to purchase from. In view of this, a bicycle factory alone (Bicyclefactory) is unable to provide all the bikes needed. So we consider designing Bicycleshop as an abstract class that allows subclasses to work on their inbound bikes (createbicycle) according to their respective purchase channels:

varBicycleshop =function () {}; Bicycleshop.prototype={sellbicycle:function (model) {//let its subclasses do the work.        varBicycle = This. Createbicycle (model); //The following code is still unchanged//Bicycle Assemblybicycle.assemble (); //Bicycle CleaningBicycle.wash (); returnbicycle; },    //abstract method, which must be implemented before calling thecreatebicycle:function (model) {Throw NewError ("You must pass an instance of the subclass to call this method, inbound bikes"); }};

A createbicycle method is defined in the Bicycleshop class that, once called, throws an exception, so the class cannot be instantiated directly, and the abstract class must be inherited to complete the work of createbicycle through its subclasses.

Factory mode Implementation

The following defines two subclasses, one subclass representing the stores purchased from the Oracle company, and one subclass representing the stores purchased from IBM, with the following code:

//Oracle Bike StorevarOraclebicycleshop =function () {};//Inherit parent classinherits (Oraclebicycleshop,bicycleshop);//implementing the Createbicycle methodOracleBicycleShop.prototype.createBicycle =function (model) {varbicycle; //Production of Bicycles    Switch(model) { Case "Speedster": Bicycle=NewOraclespeedster ();  Break;  Case "lowrider": Bicycle=NewOraclelowrider ();  Break;  Case "Alien": Bicycle=NewOraclealien ();  Break;  Case "Comfort Cruiser":        default: Bicycle=NewOraclecomfortcruiser (); }    returnbicycle;};//IBM Bike StorevarIbmbicycleshop =function () {};//Inherit parent classinherits (Ibmbicycleshop,bicycleshop);//implementing the Createbicycle methodIBMBicycleShop.prototype.createBicycle =function (model) {varbicycle; //Production of Bicycles    Switch(model) { Case "Speedster": Bicycle=NewIbmspeedster ();  Break;  Case "lowrider": Bicycle=NewIbmlowrider ();  Break;  Case "Alien": Bicycle=NewIbmlealien ();  Break;  Case "Comfort Cruiser":        default: Bicycle=NewIbmcomfortcruiser (); }    returnbicycle;};

After a design like this, the concept of a store was created, and Oraclebicycleshop said that the Oracle Bike Store , Ibmbicycleshop represents the IBM Bike store. Now, if the user needs to buy a speedster brand of bicycles, regardless of running to that store, can buy, the sample code is as follows:

 //  to Oracle store buy   var  oracle = new   Oraclebicycleshop ();  var  yournewbicycle = oracle.createbicycle ( "  speedster   "  ); //  buy from IBM store  var  IBM = new   Ibmbicycleshop ();  var  mynewbicycle = ibm.createbicycle ( " Span style= "COLOR: #800000" >speedster   "); 

Even if you want to increase support for other manufacturers, it is very simple, just need to create a bicycleshop subclass and implement its Createbicycle method.

Depending on the needs of the various sub-categories are modified to support more manufacturers of other models of products, which is the most important feature of the engineering design model.

JS design mode--Detailed Factory mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.