JavaScript Design Pattern Classic Factory pattern _javascript skills

Source: Internet
Author: User

First, the concept of factory model

The factory pattern defines an interface for creating objects that the subclass determines which class to instantiate. This pattern delays the instantiation of a class to a subclass. A subclass can override an interface method to specify its own object type (abstract factory) when it is 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 a factory method in a program that allows subclasses to define the type of object that needs to be created.

Second, the role of the factory model and matters needing attention

Mode function:

1, the object construction is very complex--we wear shoes is very simple, but the process of making shoes is very complicated

2, need to rely on a specific environment to create a different example-the factory can make shoes, make clothes, the factory can do I need shoes (shoes are different), and then sent to the designated place (place can be different), can be understood as a different example

3, processing a large number of small objects with the same attributes-such as buying a pair of shoes, there is no need to find factory production

Precautions:

1, can not abuse the factory, and sometimes just to increase the complexity of the code-as above 3

Third, factory model code and actual combat summary

<! DOCTYPE html>
 
 

The above is a small set to introduce the JavaScript design model of the classic factory model, I hope to help you!

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.