Introduction to the factory method mode of the JavaScript design mode, and the javascript Design Mode
1. Simple factory Model
Note: A factory class is created to create the implementation class for the same interface.
But JavaScript seems to have no interface, so we should remove the interface layer. Of course, the member variables in the implementation class here should be the same;
For example, here is an example of text message sending and email sending;
1>. Mail sending [Implementation] class
Copy codeThe Code is as follows:
Function MailSender (){
This. to = '';
This. title = '';
This. content = '';
}
MailSender. prototype. send = function (){
// Send body
}
2>. Text message sending [Implementation] class
Copy codeThe Code is as follows:
Function SmsSender (){
This. to = '';
This. title = '';
This. content = '';
}
SmsSender. prototype. send = function (){
// Send body
}
3>. Create a factory class:
Copy codeThe Code 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 = 'ms '){
Me. sender = new SmsSender ();
}
Return me. sender;
}
4>. Use this factory class:
Copy codeThe Code 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 Modes
Note: Multiple factory mode methods are used to improve the common factory method, because the returned result is returned based on the characters passed in. When the character input is incorrect, it may not be able to be processed, or it can be handled in an incorrect way. Multiple factory mode methods can avoid such errors;
We have made improvements to the above factory categories:
Copy codeThe Code 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;
}
Usage:
Copy codeThe Code is as follows:
Var factory = new SendFactory ();
Var sender = factory. produceSms (); // produceMail
Sender. to = 'toname # xxxxx ';
Sender. title = 'text message sending method title ';
Sender. content = 'send content ';
Sender. send ();
3. Static factory method mode
Note: you only need to change the method of multiple factory method modes to static identifiers so that SendFactory does not have to be instantiated;
Modify the Factory Code as follows:
Copy codeThe Code is as follows:
Var SendFactory = {
ProduceMail: function (){
Return new MailSender ();
},
ProduceSms: function (){
Return new SmsSender ();
}
}
Usage:
Copy codeThe Code is as follows:
Var sender = SendFactory. produceMail ();
Sender. to = 'toname # mail.com ';
Sender. title = 'email sending title ';
Sender. content = 'send content ';
Sender. send ();
Factory method mode description
In object-oriented programming, many products (real-world models, names: Class names, member attributes, and operation methods) need to be initialized, that is, products need to be created, in addition, you can use the factory method mode when [implementing the same interface]. In the first mode, the input type may be incorrect. In the second mode, you can create a factory instance when necessary;