Factory pattern of javascript design pattern, design pattern factory

Source: Internet
Author: User

Factory pattern of javascript design pattern, design pattern factory
Overview

Factory mode is a creation mode to create objects. It is usually implemented in static methods of classes or classes.

1. When a similar object is created, repeat the operation.

2. provide an interface for the factory customers to create objects when the compilation does not know the specific type.

Factory Model

Objects Created using factory methods (or classes) inherit the same parent Object Concept in design. They are all specific sub-classes that implement special functions.

Example:

(1) CarMaker, a common Constructor

(2) A static method named CarMaker of factory (), which is used to create a car object

(3) Specialized constructors inherited from CarMaker, CarMaker. Compact, and CarMaker. SUV. All these constructors are defined as static attributes of the parent class to protect the global namespace from pollution.

Implementation:

var corolla=CarMaker.factory('Compact'),      cheroke=CarMaker.factory("SUV");corolla.drive();cheroke.drive();


The factory method accepts the type specified in the form of a string at runtime, and then creates and returns the object of the requested type. No constructor with the new or object literal is found in the Code.

The following is an example of factory mode implementation, which will make the previous code snippet run normally:


 function CarMaker(){}    CarMaker.prototype.drive=function(){        return "I have "+this.doors+" doors";    }    CarMaker.factory=function(type){        var constr=type,                newcar;        if(typeof CarMaker[constr]!=="function"){            throw{                name:"Error",                message:constr+"doesn't exist"            };            if(typeof CarMaker[constr].prototype.drive!=="function"){                CarMaker[constr].prototype=new CarMaker();            }            newcar=new CarMaker[constr]();            return newcar;        }    }    CarMaker.Compact= function () {        this.doors=4;    }    CarMaker.SUV= function () {        this.doors=24;    }

When to use the factory model:

(1) When the object or component settings involve high complexity

(2) When You Need to easily produce different instances of objects in different environments

(3) when processing many small objects or components that share the same attributes


Abstract Factory)

Abstract Factory is used to encapsulate a group of single factories with common goals. It separates the implementation details of a group of objects from the general syntax.

Application abstraction factory scenario: A system must be independent of the Generation Method of the objects it creates, or it must work with multiple object types.


In the following example, a vehicle factory defines a method for obtaining or registering a vehicle type. The abstract factory is named AbstractVehicleFactory. The abstract factory will allow objects such as car or truck to be defined. A specific factory only needs to implement the Vehicle contract class (such as Vehicle. prototype. drive)


Implementation Code

Function Car (options) {this. doors = options. door | 4;} Car. prototype. drive = function () {return this. doors;} var AbstractVehicleFactory = (function () {// Abstract Factory var types ={}; return {getVehicle: function (type, mizmizations) {var Vehicle = types [type]; return (Vehicle )? (New Vehicle (mizmizations): null;}, registerVehicle: function (type, Vehicle) {var proto = Vehicle. prototype; if (proto. drive) {types [type] = Vehicle;} return AbstractVehicleFactory ;}}) (); AbstractVehicleFactory. registerVehicle ("car", Car); var car = AbstractVehicleFactory. getVehicle ("car", {door: 4}); console. log (car );





Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.