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