[design mode] JavaScript abstract Factory mode

Source: Internet
Author: User

Abstract Factory mode Description

1. Factory method Mode problem: In the Factory method mode, the creation of the class is required through the factory class, if you want to extend the program, you must modify the factory class, which violates the closure principle, open to the extension, close to the modification, for the design has some problems.

2. How to solve: it is necessary to use the abstract factory model, is to create a separate factory class function, so that you do not have to modify the previous code, but also extend the functionality.

3. Factory mode is actually a unified factory way to implement the same interface to create the call, but JavaScript has no interface this thing, so remove this layer of implementation, but the BIT function class members and methods should be the same;

Abstract Factory Source code example

1. Mail Sending class:

function MailSender () {    this. to = ';      this. title = ";     this. Content = 'function'() {    //send Body}

2. SMS Sending class:

function Smssender () {    this. to = ';      this. title = ";     this. Content = 'function'() {    //send Body}

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

1>. Mail Factory class:

function mailfactory () {    function() {    returnnew  MailSender ();}

2>. SMS Factory class:

function smsfactory () {    function() {    returnnew  Smssender ();}

4. How to use:

var New mailfactory ();
var = ' toname#mail.com '= ' abstract Factory mode '= ' send content '; sender.send ();

Other Notes

In the object-oriented language such as Java,.net C # used in the Factory mode, all use the interface, interface is exposed to a variety of users of the available methods, explain how this function application of some of the methods of application, how users should use this interface. Objects in the form of classes, representing some kind of abstraction in the real world, perhaps there are many similar applications, such as the above mail sending, text messaging, and so on, such as shopping malls in various promotional means, as well as the animal world of various animals and so on.

If we do not provide the user with the interface form, it is bound to provide the user with exposing the Real function class object, the user can arbitrarily modify and extend the class object, which is not allowed.

Factory method mode and abstract Factory mode can be a good solution to this problem, the user can only use the interface call factory class, to do the specified operation; abstract Factory mode it is easier to use extended functions, and the function classes and factory classes implement their class-level extensions on the corresponding interfaces. No modification to other classes or methods is involved;

Of course, JavaScript has no way of doing this, programmers have to discipline themselves!

[design mode] JavaScript abstract Factory mode

Related Article

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.