1. Simple factory mode
Description: It is to create a factory class, which implements the creation of the implementation class for the same interface.
But it seems that JavaScript does not have an interface number, so we remove the interface layer; of course, the member variables and methods of our implementation class should be the same;
For example: At this time, you can give an example of sending text messages and sending emails;
1>. Mail sending [implementation] class
The code copy is as follows:
function MailSender() {
this.to = '';
this.title = '';
this.content = '';
}
MailSender.prototype.send = function() {
//send body
}
2>. SMS sending [implementation] class
The code copy is as follows:
function SmsSender() {
this.to = '';
this.title = '';
this.content = '';
}
SmsSender.prototype.send = function() {
//send body
}
3>. Create a factory class:
The code copy is 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:
The code copy is 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 mode
Note: Multiple factory mode methods are an improvement to ordinary factory methods, because the return implementation is based on the passed characters. When the character input is incorrect, it may not be processed or processed into an error; while multiple factory mode methods can avoid such errors;
We have made improvements to the above factory class:
The code copy is 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:
The code copy is as follows:
var factory = new SendFactory();
var sender = factory.produceSms(); //produceMail
sender.to = 'toName#xxxxx';
sender.title = 'SMS sending method title';
sender.content = 'send content';
sender.send();
3. Static factory method mode
Note: Change the above multiple factory method mode methods to static identification so that there is no need to instantiate SendFactory;
Modify the factory code as follows:
The code copy is as follows:
var SendFactory = {
produceMail: function() {
return new MailSender();
},
produceSms : function() {
return new SmsSender();
}
}
How to use:
The code copy is as follows:
var sender = SendFactory.produceMail();
sender.to = 'toName#mail.com';
sender.title = 'Mail Send Title';
sender.content = 'send content';
sender.send();
Factory method model description
In object-oriented thinking programming description, when there are many products (real world models, names: class names, member attributes, and operation methods, etc.) that need to be initialized, that is, the product needs to be created, and [when implementing the same interface], the factory method mode can be used; the first mode has the possibility of input type error, and the second mode, when it is necessary, the factory instance is created;