JavaScript abstract factory pattern detailing _javascript Tips

Source: Internet
Author: User

Abstract Factory mode Description

1. The problem of the factory method pattern: In the factory method pattern, the creation class all needs to go through the factory class, if wants to extend the program, must modify the factory class, this violates the closure principle, to expands opens, to the modification closes, has the certain question to the design.

2. How to solve: the use of abstract Factory mode, is the function of the class to create a separate factory class, so you do not have to modify the previous code, but also extended the functionality.

3. The factory model is in fact the implementation of the same interface to implement the unified factory way to create the call, but JavaScript does not have the interface of this thing, so remove this layer implementation, but a bit functional class members and methods should be the same;

Abstract Factory Source code example

1. Mail Delivery class :

Copy Code code as follows:

function MailSender () {
this.to = ';
This.title = ';
This.content = ';
}
MailSender.prototype.send = function () {
Send Body
}

2. SMS Send class:

Copy Code code as follows:

function Smssender () {
this.to = ';
This.title = ';
This.content = ';
}
SmsSender.prototype.send = function () {
Send Body
}

3. Here is the creation of factory interface class, here is removed; Directly create various functional plant ;

1>. Mail Factory class:

Copy Code code as follows:

function Mailfactory () {

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

2>. SMS Factory class:

Copy Code code as follows:

function SMSfactory () {

}
SmsFactory.prototype.produce = function () {
return new Smssender ();
}

4. How to use:

Copy Code code as follows:

var factory = new Mailfactory ();
var sender = Factory.produce ();
sender.to = ' toname#mail.com ';
Sender.title = ' abstract factory model ';
sender.content = ' send content ';
Sender.send ();

Other Notes

In object-oriented languages such as Java,.net C # used in the factory model, are used to interface, interface is exposed to various users of the available methods to explain the function of what the application of the method, how the user should use this interface. Objects in the form of classes, representing some kind of abstraction in the real world, perhaps the scene will have a lot of similar applications, such as the above mail send, send text messages, and then such as shopping malls in a variety of promotional means, as well as the animal world of all kinds of birds and beasts.

If we do not provide user access in the form of interfaces, it is bound to provide the user with the real function of exposing the object, and the user can modify and extend the class object at will, which is not allowed.

Factory method Patterns and abstract factory patterns can solve such problems, users can only use the interface to invoke the factory class, to do the prescribed operation; abstract Factory mode further use of extension functionality becomes easier, and both functional classes and factory classes implement the respective class-level extensions for the implementation of the corresponding interfaces. Does not involve modifying to another class or method;

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.