This article describes the javascript Abstract Factory mode in detail. For more information, see
Abstract Factory mode description
1. factory method mode: In the factory method mode, all creation classes must pass the factory class. If you want to extend the program, you must modify the factory class, which violates the closure principle, it is open to extensions and closed to modifications; it has some design problems.
2. How to solve the problem: the abstract factory mode is used to create a factory class for the function class separately, so that you do not have to modify the previous Code and extend the function.
3. the factory mode is actually to create and call a uniform factory method to implement the same interface class, but javascript does not have this interface, so this layer of implementation is removed, however, the bitwise function class members and methods should be the same;
Example of abstract factory source code
1. Mail sending:
The Code is as follows:
Function MailSender (){
This. to = '';
This. title = '';
This. content = '';
}
MailSender. prototype. send = function (){
// Send body
}
2. SMS sending class:
The Code is as follows:
Function SmsSender (){
This. to = '';
This. title = '';
This. content = '';
}
SmsSender. prototype. send = function (){
// Send body
}
3. Here is the interface class for creating a factory, which is removed here; directlyCreate a function Factory;
1>. Mail Factory:
The Code is as follows:
Function MailFactory (){
}
MailFactory. prototype. produce = function (){
Return new MailSender ();
}
2>. SMS factory type:
The Code is as follows:
Function SmsFactory (){
}
SmsFactory. prototype. produce = function (){
Return new SmsSender ();
}
4. Usage:
The Code is as follows:
Var factory = new MailFactory ();
Var sender = factory. produce ();
Sender. to = 'toname # mail.com ';
Sender. title = 'abstract factory mode ';
Sender. content = 'send content ';
Sender. send ();
Other Instructions
In object-oriented languages such as java ,. net C # uses the factory mode and interfaces, which are available methods exposed to various users. This shows how to use this function to apply some methods. Objects are represented in the form of classes, representing an abstraction in the real world. There may be many similar applications, such as sending emails, sending text messages, and various promotion methods in malls, and various birds and animals in the animal world ..
If we do not provide users with the interface form, it is bound to provide users with exposing real function class objects. Users can modify and expand 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 interfaces to call factory classes to perform specified operations. The Abstract Factory mode makes it easier to use extended functions, both the function class and the factory class implement their own class-level extension on the corresponding interface, and do not involve modification to other classes or methods;