Abstract factory pattern description
1. Problems with the factory method model: In the factory method model, creating classes requires passing through the factory class. If you want to extend the program, you must modify the factory class. This violates the closure principle, opens for extensions and closes for modifications; there are certain problems with the design.
2. How to solve it: you need to use the abstract factory model, which means creating factory classes for the functional classes separately, so that you don’t have to modify the previous code and expand the functions.
3. The factory model is actually creating calls to the unified factory method of implementing the implementation class that implements the same interface, but JavaScript does not have the interface number, 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. Email sending class :
The code copy is as follows:
function MailSender() {
this.to = '';
this.title = '';
this.content = '';
}
MailSender.prototype.send = function() {
//send body
}
2. SMS sending class:
The code copy is as follows:
function SmsSender() {
this.to = '';
this.title = '';
this.content = '';
}
SmsSender.prototype.send = function() {
//send body
}
3. This is originally a factory interface class, but it is removed here; directly create various functional class factories ;
1>. Mail factory category:
The code copy is as follows:
function MailFactory() {
}
MailFactory.prototype.produce = function() {
return new MailSender();
}
2>. SMS factory category:
The code copy is as follows:
function SmsFactory() {
}
SmsFactory.prototype.produce = function() {
return new SmsSender();
}
4. How to use:
The code copy is as follows:
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 mode used in object-oriented languages such as java and .net C# use interfaces. Interfaces are available methods to expose to various external users, explaining what methods to apply this function and how users should use this interface. Objects are expressed in the form of classes, representing some abstraction in the real world. Perhaps the scene will have many similar applications, such as email sending, SMS sending, and various promotional methods in the mall, as well as various birds and beasts in the animal world.
If we do not provide users with the user's use in the form of an interface, we will inevitably provide users with the real functional class objects, and users can modify and extend the class objects at will, which is not allowed.
The factory method mode and the abstract factory mode can solve such problems well. Users can only use the interface to call the factory class to perform specified operations; the abstract factory mode further uses the extension function, and the functional class and factory class implement the respective class-level extension on the corresponding interface, and will not involve modifying other classes or methods;