Introduction to the factory method model of JavaScript design pattern _javascript Skills

Source: Internet
Author: User

1. Simple Factory model

Description: Create a factory class that implements the creation of the implementation class for the same interface.

But it seems like JavaScript doesn't have an interface, so we remove the interface layer; Of course, the member variables under the implementation class here should all be the same;

For example: At this time, to send text messages and send examples;

1>. mail delivery [Implementation] class

Copy Code code as follows:

function MailSender () {
this.to = ';
This.title = ';
This.content = ';
}

MailSender.prototype.send = function () {
Send Body
}

2>. SMS send [Implementation] class

Copy Code code as follows:

function Smssender () {
this.to = ';
This.title = ';
This.content = ';
}

SmsSender.prototype.send = function () {
Send Body
}

3>. Create a factory class:

Copy Code code as follows:

function Sendfactory () {
This.sender = null;
}

SendFactory.prototype.produce = function (type) {
var me = this;
if (type = = ' mail ') {
Me.sender = new MailSender ();
else if (type = = ' SMS ') {
Me.sender = new Smssender ();
}
return me.sender;
}

4>. Use this factory class:

Copy Code code as follows:

var factory = new Sendfactory ();
var sender = Factory.produce (' mail '); Sms
sender.to = ' toname#mail.com ';
Sender.title = ' mail test title! ';
sender.content = ' send content ';
Sender.send ();

2. Multiple Factory method modes

Description: Multiple Factory mode method is an improvement to the common factory method, because the return implementation is based on the character returned, when the character input error, may not be able to deal with, or deal with the wrong way, and multiple Factory mode method, you can avoid such a mistake;

We make improvements to the above factory class:

Copy Code code as follows:

function Sendfactory () {
This.sender = null;
}

SendFactory.prototype.produceMail = function () {
var me = this;
Me.sender = new MailSender ();
return me.sender;
}

SendFactory.prototype.produceSms = function () {
var me = this;
Me.sender = new Smssender ();
return me.sender;
}

How to use:

Copy Code code as follows:

var factory = new Sendfactory ();
var sender = Factory.producesms (); Producemail
sender.to = ' toname#xxxxx ';
Sender.title = ' sms Send method title ';
sender.content = ' send content ';
Sender.send ();

3. Static Factory method mode

Note: The above multiple factory method mode is changed to static identification, so that it does not have to sendfactory to instantiate;

Modify the factory class code as follows:

Copy Code code as follows:

var sendfactory = {
Producemail:function () {
return new MailSender ();
},
Producesms:function () {
return new Smssender ();
}
}

How to use:

Copy Code code as follows:

var sender = Sendfactory.producemail ();
sender.to = ' toname#mail.com ';
Sender.title = ' mail send title ';
sender.content = ' send content ';
Sender.send ();

Factory Method Mode Description

In object-oriented programming instructions, there are many products (real world model, Name: Class name, member properties, and Operation methods, etc.) need to initialize, that is, the product needs to be created, and [implement the same interface], you can use the factory method mode; The first mode, there is the possibility of input type errors, the second mode, when needed to create a factory instance;

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.