The factory method pattern is an improvement to the simple factory pattern, which adds a factory class for each object that is specifically used to generate the object.
The factory method implementation subtraction example is as follows:
1 Operation class
public class Operation {public double Numbera {get; set;} Public double Numberb {get; set;} Public virtual Double GetResult () {double result = 0; return result; }} public class Operationadd:operation {public override double GetResult () {return Numbera + Numberb; }} public class Operationsub:operation {public override double GetResult () {return Numbera-numberb; }} public class Operationmul:operation {public override double GetResult () {return Numbera * NUMBERB; }} public class Operationdiv:operation {public override double GetResult () {if (Num Berb = = 0) {throw new Exception ("divisor cannot be 0"); } return Numbera/numberb; } }
2 Add a factory object for each action class
<summary>//// factory method mode moves the internal logic judgment of a simple factory to the client code, adding a method without adding conditional branches, simply adding objects and factories/// </summary> Interface Ifactory { operation createoperation (); } public class Addfactory:ifactory {public operation CreateOperation () { return new Operationadd (); } } public class Subfactory:ifactory {public operation CreateOperation () { return new operationsub ( ); } } public class Mulfactory:ifactory {public operation CreateOperation () { return new Operationmul ( ); } } public class Divfactory:ifactory {public operation CreateOperation () { return new Operationdiv ( ); } }
3 objects that need to be used for factory production at the client
Ifactory addfactory = new Addfactory (); Operation operation = Addfactory.createoperation (); operation. Numbera = 1;operation. Numberb = 2; Console.WriteLine (operation. GetResult ());
Operation Result: 3
The factory method mode moves the judgment to the client and does not solve the problem of which object to instantiate, but this pattern is of great benefit in some scenarios, such as the use of SQL Server database, after the code development is completed, if the data is changed to Oracle, just modify the instance factory, There is no need to modify other places, or to modify the instantiation process of the manipulated object, the difference between using the factory method pattern and the unused code is compared below:
private void Usefactory () {//If all additions are subtracted, modify only one sentence ifactory addfactory = new Addfactory (); Operation Operation1 = Addfactory.createoperation (); Operation1. Numbera = 1; Operation1. Numberb = 2; Console.WriteLine (Operation1. GetResult ()); Operation Operation2 = Addfactory.createoperation (); Operation2. Numbera = 5; Operation2. Numberb = 3; Console.WriteLine (Operation1. GetResult ()); Operation Operation3 = Addfactory.createoperation (); Operation3. Numbera = 6; Operation3. Numberb = 1; Console.WriteLine (Operation1. GetResult ()); private void Unusefactory () {//If all additions are subtracted and all instantiations need to be modified, the problem is also present in the simple factory operation Operatio N1 = new Operationadd (); Operation1. Numbera = 1; Operation1. Numberb = 2; Console.WriteLine (Operation1. GetResult ()); Operation Operation2 = nEW Operationadd (); Operation2. Numbera = 5; Operation2. Numberb = 3; Console.WriteLine (Operation1. GetResult ()); Operation Operation3 = new Operationadd (); Operation3. Numbera = 6; Operation3. Numberb = 1; Console.WriteLine (Operation1. GetResult ()); }
In general, factory release overcomes the shortcomings of the simple factory violation of the open-closed principle, but also preserves the advantages of the encapsulation object creation process, the process of encapsulating object creation makes the program not need big changes when changing objects, reducing the coupling degree between client and concrete object.
Factory method Mode