javascript abstract factory pattern details

  • 2020-05-05 10:53:54
  • OfStack

The abstract factory pattern illustrates

1. Problems with the factory method pattern: in the factory method pattern, classes are created by passing through the factory class, and if the program is to be extended, the factory class must be modified. There are certain problems with the design.
 
2. How to solve it: we need to use the abstract factory pattern, which is to create a separate factory class for the function class, so that we do not have to modify the previous code, and extend the function.
 
3. The factory pattern is actually to create calls to the unified factory method of the implementation class that implements the same interface. However, javascript has no such thing as interface, so this layer of implementation is removed, but the members and methods of the bit-function class should be the same.  

Abstract factory source code example

1. Mail sending class :
 


function MailSender() {
    this.to = '';
    this.title = '';
    this.content = '';
}
MailSender.prototype.send = function() {
    //send body
}

2. SMS sending :
 


function SmsSender() {
    this.to = '';
    this.title = '';
    this.content = '';
}
SmsSender.prototype.send = function() {
    //send body
}

3. The factory interface class was originally created, but it was removed. Direct to create for each functional class;
 
1 > . Mail factory class :
 


function MailFactory() {
   
}
MailFactory.prototype.produce = function() {
    return new MailSender();
}

2 > . SMS factory :


function SmsFactory() {
   
}
SmsFactory.prototype.produce = function() {
    return new SmsSender();
}

4. Method of use:


var factory = new MailFactory();
var sender = factory.produce();
sender.to = 'toname#mail.com';
sender.title = ' Abstract factory pattern ';
sender.content = ' Send content ';
sender.send();

other instructions

The factory pattern used in object-oriented languages such as java,.net C#, USES interfaces, which are available methods that expose various users to what the functionality is applied to and how the user should use the interface. Objects are represented in the form of classes, representing a certain abstraction in the real world. Maybe there will be many similar applications in the scene, such as the above email sending, text message sending, and then such as all kinds of promotional means in the mall, as well as all kinds of animals in the animal world.

If we do not provide the user with the form of interface, it is bound to provide exposed real function class object to the user, the user can modify and extend the class object at will, which is not allowed.

The factory method pattern and the abstract factory pattern can solve this problem very well, the user can only use the interface to call the factory class, to carry on the specified operation; The abstract factory pattern makes it easier to further use the extension functionality. Both the function class and the factory class implement their own class-level extensions on the corresponding interfaces, without any modification to other classes or methods.


Related articles: