In-depth understanding of the JavaScript series (28): The factory pattern of design patterns

Source: Internet
Author: User

Introduced

Similar to the create pattern, the Factory mode creates objects (which are considered products in the factory) without specifying a specific class to create the object.

The factory pattern defines an interface for creating objects, which subclasses decide which class to instantiate. This pattern defers the instantiation of a class to a subclass. Subclasses can override interface methods to specify their own object types when they are created.

This pattern is useful, especially when creating an object's process assignment, such as relying on many settings files. Also, you will often see the factory method in the program, which allows the subclass class to define the type of object that needs to be created.

Body

In the following example, an improved version of the constructor pattern code is applied to the 26th chapter of the factory method:

varCar = (function() {varCar =function(model, year, miles) { This. model = model; This. year = year; This. Miles = Miles; };return function(model, year, miles) {return NewCar (model, year, miles); };}) ();varTom =NewCar ("Tom", 2009, 20000);varDudu =NewCar ("Dudu", 2010, 5000);

If we don't understand it, we'll give you another example:

var  Productmanager = {};p roductmanager.createproducta =  function  () {console.log (' producta ');}    PRODUCTMANAGER.CREATEPRODUCTB = function  () {        Console.log (' PRODUCTB ');} Productmanager.factory = function  (typeType) { Span style= "Color:rgb (0,0,255); Line-height:1.5!important ">return  new  Productmanager[typetype];} Productmanager.factory ("Createproducta"); 

If you do not understand, then we will be a little more detailed, if we want to insert some elements in the Web page, and these elements are not fixed, may be a picture, it may be connected, or even text, according to the definition of Factory mode, we need to define the factory class and the corresponding sub-class, Let's first define the specific implementation of the subclass (that is, the child function):

varpage = Page | | {};p age.dom = Page.dom | | {};//Child function 1: Working with textPage.dom.Text =function() { This. Insert =function(where) {vartxt = document.createTextNode ( This. URL);    Where.appendchild (TXT); };};//Child function 2: Working with linksPage.dom.Link =function() { This. Insert =function(where) {varlink = document.createelement (' a '); Link.href = This. URL; Link.appendchild (document.createTextNode ( This. url));    Where.appendchild (link); };};//Child function 3: working with picturesPage.dom.Image =function() { This. Insert =function(where) {varim = document.createelement (' img '); IM.SRC = This. URL;    Where.appendchild (IM); };};

So how do we define the factory handler function? It's actually very simple:

function (type) {    returnnew page.dom[type];}

Use the following methods:

var o = page.dom.factory (' Link '); o.url = ' http://www.cnblogs.com '; O.insert (document.body);

At this point, the introduction of the factory model is believed that everyone has been clear in mind, I will no longer narrate.

Summarize

When to use Factory mode

The following scenarios are particularly useful for the factory model:

    1. Objects are very complex to build
    2. Need to rely on a specific environment to create different instances
    3. Handle large numbers of small objects with the same properties

When should I not use Factory mode?

Not abusing the factory model, sometimes simply adding unnecessary complexity to the code and making the test difficult to run.

Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.

In-depth understanding of the JavaScript series (28): The factory pattern of design patterns

Related Article

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.