[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, closed 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 the implementation of the same interface to implement the unified factory way to create the call, but JavaScript does not have an 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 Send class:  copy code function MailSender () {    this.to = ';    this.title = ';    this.content = ';}  mailsender.prototype.send = function () {   //send Body} copy code 2. SMS Send class:  copy code function Smssender () {    this.to = ';    this.title = ';    this.content = ';}  smssender.prototype.send = function () {   //send Body} copy code 3. This would have been to create a factory interface class, where it was removed; Directly create each function class factory;  1>. Mail Factory class:  function mailfactory () {    }mailfactory.prototype.produce = function () {    return new MailSender ();} 2>. SMS Factory class:  function smsfactory () {    }smsfactory.prototype.produce = function () {    return New SmsSender ();} 4. How to use:  var factory = new Mailfactory (); var sender = Factory.produce (); sender.to = ' toname#mail.com '; sender.title = ' Abstract Factory mode '; sender.content = ' send content '; Sender.send (); Other notes   in object-oriented languages such as Java,.net C # Use of the Factory mode, the interface is the external to a variety of user exposure to the available methods, Explain how this function applies some of the methods used, how the user 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 users in the form of interface, it is bound to provide exposing the real function class object to the user, 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;

[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.