Home > Web Front-end > JS Tutorial > Introduction to factories in JavaScript design patterns

Introduction to factories in JavaScript design patterns

不言
Release: 2018-07-03 14:52:08
Original
1268 people have browsed it

This article mainly introduces the factory pattern of JavaScript design patterns. It analyzes the concept and principle of the factory pattern in the form of complete examples, as well as the definition of javascript and the related operating skills of using the factory pattern. Friends in need can refer to it

The examples in this article describe the factory pattern of JavaScript design patterns. Share it with everyone for your reference, as follows:

1. Factory pattern concept

The factory pattern defines an interface for creating objects. This interface determines which class to instantiate by the subclass. This pattern defers instantiation of a class to subclasses. Subclasses can override interface methods to specify their own object types (abstract factory) when creating.

This mode is very useful, especially when assigning values ​​to the process of creating objects, such as relying on many settings files. Moreover, you will often see factory methods in programs, which are used to let subclasses define the types of objects that need to be created.

2. The functions and precautions of the factory mode

The functions of the mode:

1. The object construction is very complex-it is very simple for us to put on shoes, but the process of making shoes is very Complex

2. Different instances need to be created depending on the specific environment - the factory can make shoes and clothes, the factory can make the shoes I need (the shoes are different), and then send them to the designated place (the places can be different) ), can be understood as different instances

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 a factory to produce

Notes:

1. Don’t abuse the factory. Sometimes it just adds complexity to the code - as above 3

3. Factory pattern code and practical summary

<!DOCTYPE html>
<html>
<head lang="en">
 <meta charset="UTF-8">
 <title>工厂模式</title>
</head>
<body>
<script>
 //1.工厂应该有厂长来决定运行到底哪条产品线
 //2.消费者-》子类
 var gongchang = {};
 gongchang.chanyifu = function(){
  this.gongren = 50;
  console.log("我们有"+this.gongren);
 }
 gongchang.chanxie = function(){
  this.gongren = 100;
  console.log("产鞋子");
 }
 gongchang.yunshu = function(){
  this.gongren = 10;
  console.log("运输");
 }
 gongchang.changzhang = function(para){
  return new gongchang[para]();
 }
 var we = gongchang.changzhang("chanyifu");
 var me = gongchang.changzhang("chanxie");
 console.log(me.gongren);
 var ys = gongchang.changzhang("yunshu");
 console.log(ys.gongren);
</script>
</body>
</html>
Copy after login

The operation effect is as follows:

The above is the entire content of this article. I hope it will be helpful to everyone's study. Please pay attention to more related content. PHP Chinese website!

Related recommendations:

Introduction to the proxy mode in JavaScript design patterns

Two ways to use Echarts in Vue introduce

The above is the detailed content of Introduction to factories in JavaScript design patterns. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template