The biggest advantage of the simple factory mode is that the factory class contains the necessary logic judgment, and the related classes are dynamically instantiated Based on the client selection conditions. For the client, the dependencies with specific products are removed. For example, the factory function that appears in the simple factory mode:
Operation* FactoryFunction(double left, double right, char op){ switch (op) { case '+': return new Add(left, right); break; case '-': return new Sub(left, right); break; case '*': return new Mul(left, right); break; case '/': return new Div(left, right); break; default: throw runtime_error("Operation invalid!"); break; }}
Select a required operator and pass it to the factory function. The factory function instantiates a corresponding Operator Based on the selection and returns it to the customer program. The advantage here is that the detailed processing is handed over to the factory functions, which are transparent to user programmers. But there are also disadvantages: When we need to add new operation operations, we need to add the case Branch statement in the factory function, which violates the principle of open and closed. The principle of open and closed means that the software entity should be scalable but not changeable. To avoid violating the open and closed principle, you can use the factory method mode: Define an interface for creating objects, and let the subclass decide which class to instantiate. The following is a calculator class written in C ++ that uses the factory method mode.
# Include <iostream> using namespace STD; // class operation {public: Operation (const double left, const double right) {LHS = left; RHS = right ;} virtual double calculate () = 0; // pure virtual function protected: Double LHS; double RHS ;}; // addition operation class Add: Public operation {public: add (double left, double right): Operation (left, right) {}double calculate () {return LHS + RHS ;}; // subtraction class sub: Public operation {public: sub (double left, double right): Operation (left, right) {} double calculate () {return LHS-RHS ;}; // multiplication class Mul: public operation {public: MUL (double left, double right): Operation (left, right) {}double calculate () {return LHS * RHS ;}}; // Division calculation class Div: Public operation {public: div (double left, double right): Operation (left, right) {try {If (Right = 0) throw runtime_error ("the divisor cannot be 0 \ n");} catch (const runtime_error & E) {cout <E. what () <Endl; throw ;}} double calculate () {return LHS/RHS ;}; // abstract factory class abstractfactory {public: virtual Operation * createoperation (const int left, const int right) = 0 ;}; // The factory class addfactory: Public abstractfactory {public: operation * createoperation (const int left, const int right) {return new add (left, right) ;}; class subfactory: Public abstractfactory {public: operation * createoperation (const int left, const int right) {return new sub (left, right) ;}}; class mulfactory: Public abstractfactory {public: operation * createoperation (const int left, const int right) {return New MUL (left, right) ;}; class divfactory: Public abstractfactory {public: operation * createoperation (const int left, const int right) {return New Div (left, right) ;}; int main () {abstractfactory * Creator = new addfactory (); operation * Add = creator-> createoperation (11, 22); cout <add-> calculate () <Endl; Delete creator; creator = new subfactory (); operation * sub = creator-> createoperation (25, 32); cout <sub-> calculate () <Endl; Delete creator; creator = new mulfactory (); operation * Mul = creator-> createoperation (11, 11); cout <mul-> calculate () <Endl; Delete creator; creator = new divfactory (); operation * DIV = creator-> createoperation (50, 8); cout <div-> calculate () <Endl; Delete creator; // do not forget to destroy the pointer Delete add; delete sub; Delete Mul; Delete div; System ("pause"); Return 0 ;}
Running result:
In the above example, A abstractfactory abstract factory class is defined. The factory class that creates the actual object must inherit the abstract base class and override the createoperation method of the specific object generation method. When a new operation is added, you do not need to modify the original factory class. Instead, you only need to inherit abstractfactory to define a new factory class, which conforms to the closed and open principle.
Refer:
Chapter 2 of "big talk Design Model"