Home > Web Front-end > JS Tutorial > body text

Detailed description of javascript abstract factory pattern_javascript skills

WBOY
Release: 2016-05-16 16:26:33
Original
1539 people have browsed it

Abstract Factory Pattern Description

1. Problems with the factory method pattern: In the factory method pattern, creating a class requires passing the factory class. If you want to extend the program, you must modify the factory class. This violates the closure principle and is open to extension but closed to modification. ;There are certain problems with the design.

2. How to solve: We need to use the abstract factory pattern, which is to create a factory class separately for the functional class, so that there is no need to modify the previous code and the function is expanded.

3. The factory pattern is actually a unified factory method for creating and calling implementation classes that implement the same interface. However, JavaScript does not have such a thing as an interface, so this layer of implementation is removed, but the members and methods of the functional classes should be the same;

Abstract factory source code example

1. Email sending class:

Copy code The code is as follows:

function MailSender() {
This.to = '';
This.title = '';
This.content = '';
}
MailSender.prototype.send = function() {
//send body
}

2. SMS sending category:

Copy code The code is as follows:

function SmsSender() {
This.to = '';
This.title = '';
This.content = '';
}
SmsSender.prototype.send = function() {
//send body
}

3. The factory interface class was originally created here, but it is removed here; directly create various functional class factories;

1>. Mail factory class:

Copy code The code is as follows:

function MailFactory() {
 
}
MailFactory.prototype.produce = function() {
Return new MailSender();
}

2>. SMS factory class:

Copy code The code is as follows:

function SmsFactory() {
 
}
SmsFactory.prototype.produce = function() {
Return new SmsSender();
}

4. How to use:

Copy code The code is as follows:

var factory = new MailFactory();
var sender = factory.produce();
sender.to = 'toname#mail.com';
sender.title = 'Abstract Factory Pattern';
sender.content = 'Send content';
sender.send();

Other instructions

The factory pattern used in object-oriented languages ​​such as java and .net C# all uses interfaces. Interfaces are available methods exposed to various users. They describe what methods are used to apply this function and how users should use it. interface. Objects are expressed in the form of classes, representing some kind of abstraction in the real world. Maybe the scene has many similar applications, such as the above email sending, SMS sending, various promotional methods in shopping malls, and the animal world. Various birds and animals, etc..

If we do not provide users with interfaces, we will inevitably provide real functional class objects to users. Users can modify and extend class objects at will, which is not allowed.

Factory method pattern and abstract factory pattern can solve this problem very well. Users can only use the interface to call the factory class to perform specified operations; the abstract factory pattern further makes it easier to use extended functions. Function classes and factories Classes implement their own class-level extensions on the corresponding interfaces, and will not involve modifying other classes or methods;

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!