Abstract Factory Pattern Description
1. Problems with the factory method pattern: In the factory method pattern, creating a class requires passing the factory class. If you want to extend the program, you must modify the factory class. This violates the closure principle and is open to extension but closed to modification. ;There are certain problems with the design.
2. How to solve: We need to use the abstract factory pattern, which is to create a factory class separately for the functional class, so that there is no need to modify the previous code and the function is expanded.
3. The factory pattern is actually a unified factory method for creating and calling implementation classes that implement the same interface. However, JavaScript does not have such a thing as an interface, so this layer of implementation is removed, but the members and methods of the functional classes should be the same;
Abstract factory source code example
1. Email sending class:
2. SMS sending category:
3. The factory interface class was originally created here, but it is removed here; directly create various functional class factories;
1>. Mail factory class:
2>. SMS factory class:
4. How to use:
Other instructions
The factory pattern used in object-oriented languages such as java and .net C# all uses interfaces. Interfaces are available methods exposed to various users. They describe what methods are used to apply this function and how users should use it. interface. Objects are expressed in the form of classes, representing some kind of abstraction in the real world. Maybe the scene has many similar applications, such as the above email sending, SMS sending, various promotional methods in shopping malls, and the animal world. Various birds and animals, etc..
If we do not provide users with interfaces, we will inevitably provide real functional class objects to users. Users can modify and extend class objects at will, which is not allowed.
Factory method pattern and abstract factory pattern can solve this problem very well. Users can only use the interface to call the factory class to perform specified operations; the abstract factory pattern further makes it easier to use extended functions. Function classes and factories Classes implement their own class-level extensions on the corresponding interfaces, and will not involve modifying other classes or methods;