JavaScript design mode: Classic factory mode and javascript Design Mode

Source: Internet
Author: User

JavaScript design mode: Classic factory mode and javascript Design Mode

I. Concept of factory Model

The factory mode defines an interface for creating objects. The subclass determines which class to instantiate. This mode delays the instantiation of a class to the subclass. Subclass can override the interface method to specify its own object type (Abstract Factory) during creation ).

This mode is very useful, especially when the process of creating an object is assigned a value, for example, depending on many configuration files. In addition, you will often see the factory method in the program to let the subclass define the object type to be created.

Ii. Functions and precautions of the factory Model

Role of the Mode:

1. Object Construction is very complex-we are very simple in shoes, but the process of shoes production is very complicated.

2. Depending on the specific environment to create different instances-factories can make shoes, make clothes, factories can make the shoes I need (different shoes ), it is then delivered to the specified place (the place can be different) and can be understood as different instances.

3. Deal with a large number of small objects with the same attributes-for example, buy a pair of shoes, there is no need to find a factory for Production

Note:

1. Do not abuse the factory. Sometimes it only adds complexity to the code -- above 3

Iii. Factory model code and practical summary

<! DOCTYPE html> 

The above is the typical factory model of the JavaScript design model introduced by the small Editor. I hope it will help you!

Articles you may be interested in:
  • Sample factory mode for javascript Design Patterns
  • Introduction to abstract factory patterns in JavaScript Design Patterns
  • Factory mode and constructor mode in JavaScript Design Mode
  • In-depth understanding of the JavaScript series (28): Detailed description of the factory model of the design model
  • Javascript Design Pattern Theory and programming practices-simple factory pattern

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.