Implementation of multi-state factory-factory method mode
Overview of factory method Modes
In the factory method mode, a unified factory class is no longer provided to create all product objects, but different factories are provided for different products, the system provides a factory level structure corresponding to the product level structure. The factory method mode provides an abstract factory interface to declare abstract factory methods, especially subclass to implement factory methods and create specific product objects. Factory method mode structure
The factory mode diagram contains the following four roles. (1) Abstract product: it is the interface that defines the product and the super type of the object created in the factory method mode. (2) specific products: it implements abstract product interfaces. A specific type of product is created by a specific factory. The specific factory corresponds to a specific product one by one. (3) Abstract Factory: In the abstract factory class, the factory method is declared to return the product. Abstract Factory is the core of the factory method mode. All factory classes that create objects must implement this interface. (4) specific factory: it is a subclass of the abstract factory. It implements the factory method defined in the abstract factory and can be called by the client to return a specific product class. Compared with the simple factory model, the most important difference between the factory method model is the introduction of abstract factory roles.
Heavy Load factory Method
You can provide a set of reload factory methods to create product objects in different ways. Of course, for a specific factory, no matter which heavy load factory method is used, the product types created must be the same.
Hiding factory methods
In the factory class, you can directly call the business methods of the product class. The client can use the business methods of the created object directly through the factory without calling the factory method to create a product. The following is an example of the abstract factory LoggerFactory code:
abstract class LoggerFactory{public void writeLog(){Logger logger=this.createLogger();logger.writeLog();}public abstract LoggerFactory createLogger();}
Summary of factory method modes main advantages
1. when adding a new product to the system, you do not need to modify the interfaces provided by the abstract factory or abstract product. You do not need to modify the client or other specific products or factories, you only need to add a specific factory and a specific product. The system scalability number conforms to the principle of opening and closing.
Main disadvantage 1. when adding a new product, you need to add a new product class and a specific factory. The number of classes in the system will increase in pairs, increasing the complexity of the system to a certain extent, more classes need to be compiled and run, which brings additional overhead to the system.
Scenario 1: The abstract factory class uses its subclass to specify the object to be created.
Design of application instance logger in factory method mode
The sample code is as follows:
# Include
Using namespace std; // log Recorder Interface: Abstract product class Logger {public: virtual void writeLog () = 0 ;}; // database log recorder class DatabaseLogger: public Logger {public: void writeLog () {cout <Database Logger <
CreateLogger (); logger-> writeLog (); return 0 ;}