JS's understanding of the factory model

Source: Internet
Author: User

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

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.