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;