Home Web Front-end JS Tutorial Detailed explanation of factory pattern of javascript pattern design_javascript skills

Detailed explanation of factory pattern of javascript pattern design_javascript skills

May 16, 2016 pm 06:27 PM
Factory pattern

Pattern type: Factory pattern
Pattern description: One of the common patterns, used to dynamically create objects
Scope of application: Classes that need to choose among a series of interchangeable subclasses during runtime
Notes : The implementation of the interface, so that different subclasses can be treated equally, use the factory pattern appropriately, but do not stick to the form and understand the essence.
Key points: Selectors built with functions/classes/subclasses
Essence: the use of functions as selectors
General usage form:
Exists as an independent selector:

Copy code The code is as follows:

function FactoryMode(index){
switch(index){
case "index1" :
return new Class1();break;
case "index2":
return new Class2();break;
case "index3":
return new Class3( );break;
default:return new ClassComm();break;
}
}

or exist as a method of the class:
Copy code The code is as follows:

var MainClass=function(){};//Main class constructor
MainClass. prototype={
FactoryMode:function(){}//Subclass selector
}

Or implicit selection, that is, it is not selected based on the user's subjective choice:

Copy code The code is as follows:

var xmlRequest=function(){
if(this.isOffOnline()){
xhr= new OfflineHandler();
}//If the network is not available at this time, create a cacheable AJAX object
else if(this.isHightLatency()){
xhr= new QueuedHandler();
}//If the network delay is large, create a queue form AJAX object
else {
xhr=new SimpleHandler();
}//If the network Normally, create a simple AJAX object
interface.ensureImplements(xhr,AjaxHandler);
//Check whether the object implements the interface to ensure that future work can proceed smoothly
return xhr;
}

Extension:

The essence of the factory pattern is the application of selectors. Selectors can not only be used as object selection, but also function selection, class selection, and parameter selection
Selection of functions, such as:
Copy code The code is as follows:

var addEvent=(function (){
if(!-[0,]){
return function(elem,type,handler){
elem[type handler.toString()]=handler;
elem.attachEvent ("on" type,elem[type handler.toString]);
}}//if IE
else {
return function(elem,type,handler){
elem.addEventListener(type ,handler,false);
}
}
})();//Avoid multiple judgments of

class selection:

Copy code The code is as follows:

var suitableClass=function(){
if(match condition A) return Class1 ;
else if(match condition B) return Class2;
else return ClassComm;
}

Parameter selection:

Copy code The code is as follows:

function Country(country){
if(country=="China")
this.config={};//Set basic parameter 1
else if(contry=="America")
this.config={};//Set parameter 2
else if()
..........//Wait
}
Country.prototype={};
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the benefits of java factory pattern What are the benefits of java factory pattern Dec 25, 2023 pm 05:40 PM

The benefits of the Java factory pattern: 1. Reduce system coupling; 2. Improve code reusability; 3. Hide the object creation process; 4. Simplify the object creation process; 5. Support dependency injection; 6. Provide better performance; 7. Enhance testability; 8. Support internationalization; 9. Promote the open and closed principle; 10. Provide better scalability. Detailed introduction: 1. Reduce the coupling of the system. The factory pattern reduces the coupling of the system by centralizing the object creation process into a factory class; 2. Improve the reusability of code, etc.

What are the application scenarios of factory pattern in java framework? What are the application scenarios of factory pattern in java framework? Jun 01, 2024 pm 04:06 PM

The factory pattern is used to decouple the creation process of objects and encapsulate them in factory classes to decouple them from concrete classes. In the Java framework, the factory pattern is used to: Create complex objects (such as beans in Spring) Provide object isolation, enhance testability and maintainability Support extensions, increase support for new object types by adding new factory classes

How to apply factory pattern in Golang How to apply factory pattern in Golang Apr 04, 2024 am 11:33 AM

Factory Pattern In Go, the factory pattern allows the creation of objects without specifying a concrete class: define an interface (such as Shape) that represents the object. Create concrete types (such as Circle and Rectangle) that implement this interface. Create a factory class to create objects of a given type (such as ShapeFactory). Use factory classes to create objects in client code. This design pattern increases the flexibility of the code without directly coupling to concrete types.

An in-depth analysis of the Java factory pattern: distinguishing and applying the differences between simple factories, factory methods and abstract factories An in-depth analysis of the Java factory pattern: distinguishing and applying the differences between simple factories, factory methods and abstract factories Dec 28, 2023 pm 03:09 PM

Detailed explanation of Java Factory Pattern: Understand the differences and application scenarios of simple factories, factory methods and abstract factories Introduction In the software development process, when faced with complex object creation and initialization processes, we often need to use the factory pattern to solve this problem. As a commonly used object-oriented programming language, Java provides a variety of factory pattern implementations. This article will introduce in detail the three common implementation methods of the Java factory pattern: simple factory, factory method and abstract factory, and conduct an in-depth analysis of their differences and application scenarios. one,

The application of singleton mode and factory mode in C++ function overloading and rewriting The application of singleton mode and factory mode in C++ function overloading and rewriting Apr 19, 2024 pm 05:06 PM

Singleton pattern: Provide singleton instances with different parameters through function overloading. Factory pattern: Create different types of objects through function rewriting to decouple the creation process from specific product classes.

PHP Design Patterns: The Path to Code Excellence PHP Design Patterns: The Path to Code Excellence Feb 21, 2024 pm 05:30 PM

Introduction PHP design patterns are a set of proven solutions to common challenges in software development. By following these patterns, developers can create elegant, robust, and maintainable code. They help developers follow SOLID principles (single responsibility, open-closed, Liskov replacement, interface isolation and dependency inversion), thereby improving code readability, maintainability and scalability. Types of Design Patterns There are many different design patterns, each with its own unique purpose and advantages. Here are some of the most commonly used PHP design patterns: Singleton pattern: Ensures that a class has only one instance and provides a way to access this instance globally. Factory Pattern: Creates an object without specifying its exact class. It allows developers to conditionally

Understand the factory pattern in PHP object-oriented programming Understand the factory pattern in PHP object-oriented programming Aug 10, 2023 am 10:37 AM

Understanding the Factory Pattern in PHP Object-Oriented Programming The factory pattern is a commonly used design pattern that is used to decouple the creation and use of objects during the process of creating objects. In PHP object-oriented programming, the factory pattern can help us better manage the creation and life cycle of objects. This article will introduce the factory pattern in PHP in detail through code examples. In PHP, we can implement the object creation and initialization process by using the factory pattern instead of directly using the new keyword. The advantage of this is that if changes need to be made in the future

Detailed explanation of Java factory pattern: simple factory, factory method and abstract factory Detailed explanation of Java factory pattern: simple factory, factory method and abstract factory Dec 28, 2023 am 10:23 AM

Detailed explanation of Java factory pattern: simple factory, factory method and abstract factory The factory pattern is a commonly used design pattern. It is used to dynamically create objects according to different needs, separate the creation and use of objects, and improve the reusability and use of code. Scalability. In Java, there are three main forms of factory pattern: simple factory, factory method and abstract factory. 1. Simple factory model The simple factory model is the most basic factory model and the simplest form. It creates objects through a factory class and determines which object to create based on different parameters.

See all articles