Introduction to the factory method pattern of the JavaScript design pattern _ javascript skills

Source: Internet
Author: User
This article mainly introduces the Factory method mode of the JavaScript design mode. This article describes the simple factory mode, multiple Factory method modes, and so on. For more information, see 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

The code is as follows:


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

MailSender. prototype. send = function (){
// Send body
}

2>. text message sending [implementation] class

The code is as follows:


Function SmsSender (){
This. to = '';
This. title = '';
This. content = '';
}

SmsSender. prototype. send = function (){
// Send body
}

3>. create a factory class:

The 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:

The 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:

The 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:

The 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:

The code is as follows:


Var SendFactory = {
ProduceMail: function (){
Return new MailSender ();
},
ProduceSms: function (){
Return new SmsSender ();
}
}

Usage:

The 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;

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.