There are a lot of people on the factory model has such or not understand the place, today I will share with you my experience.
In fact, the factory model is based on an object-oriented model. Let's look at a piece of code like this:
In fact, this program is very simple, learn JS people will write, basically no problem. But you will soon realize that the problem is that if there are 100 of the objects will appear what effect, no mistake! The code will become quite much, so it will load up quite slowly, so how to solve this problem? I believe we all think of it! Yes, just wrap it up and the code looks like this:
This is the encapsulated code, in fact, this is a simple factory model, why do you say that because he works like a factory working in a process, that is: raw materials, processing, factory.
Here this big constructor Createperson () He's like a big factory, where the raw materials, processing, and factory are all visible.
To sum up: I understand the factory pattern: It is to use object-oriented approach, to encapsulate some objects, so that some of the space-intensive, repetitive code encapsulation of a pattern.
JS's understanding of the factory model