The "Factory method" of Asp. Net design pattern is the abstract factory implemented by using interfaces, and The asp.net Design Pattern
/// <Summary>
/// 6. Create a factory method mode (Abstract Factory: Interface)
/// </Summary>
Interface IFactory // parent interface (parent factory)
{
/// <Summary>
/// This parent interface provides a function method without Parameters
/// </Summary>
/// <Returns> </returns>
Operation CreateOperation ();
}
/// <Summary>
/// Define the subclass factory and inherit the parent class factory (parent interface)
/// </Summary>
Class FactoryAdd: ifacloud
{
/// <Summary>
/// Encapsulate the function with the same name as the parent class factory (the return value type is also the same)
/// </Summary>
/// <Returns> </returns>
Public Operation CreateOperation ()
{
// Return the OperationAdd subclass above
Return new OperationAdd ();
}
}
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace FactoryModel 8 {9 // ================================ use C # and use the simple factory mode, simple calculator functions =====================================10 // knowledge points during investigation: object-oriented features-inheritance, encapsulation, and polymorphism 11 /// <summary> 12 /// 1. define the parent class, and it is also an encapsulation of 13 /// </summary> 14 class Operation 15 {16 // 2. to allow the subclass to access the parent class, define the parameter as protected variable type 17 protected int numberA; 18 protected int numberB; 19 // define attributes (required) 20 public int NumberA 21 {22 get {return numberA;} 23 set {numberA = value ;} 24} 25 public int NumberB 26 {27 get {return numberB;} 28 set {numberB = value;} 29} 30 // 3. encapsulate virtual Methods for subclasses to rewrite 31 public virtual int getResule () 32 {33 int result = 0; 34 return result; 35} 36} 37 // <summary> 38 // 4. defines subclass, inherits the parent class, And overwrites the parent class (addition) 39 // </summary> 40 class OperationAdd: Operation 41 {42 public override int getResule () 43 {44 return numberA + numberB; 45} 46} 47 // <summary> 48 // 5. define subclass, inherit from parent class, and override (subtract) the parent class 49 // </summary> 50 class OperationSub: Operation 51 {52 public override int getResule () 53 {54 return numberA-numberB; 55} 56} 57 // <summary> 58 // 6. create factory method mode (Abstract Factory: interface) 59 // </summary> 60 interface ifacloud // parent interface (parent factory) 61 {62 // <summary> 63 // This parent interface provides a no-argument Function Method 64 /// </summary> 65 /// <returns> </ returns> 66 Operation CreateOperation (); 67} 68 // <summary> 69 // define the subclass factory and inherit the parent class factory (parent interface) 70 /// </summary> 71 class FactoryAdd: IFactory 72 {73 // <summary> 74 // encapsulate the function with the same name as the parent class factory (the return value type is also the same) 75 /// </summary> 76 // <returns> </returns> 77 public Operation CreateOperation () 78 {79 // return the above OperationAdd subclass 80 return new OperationAdd (); 81} 82} 83 // <summary> 84 // define the subclass factory, and inherit the parent class factory (parent interface) 85 // </summary> 86 class FactorySub: IFactory 87 {88 // <summary> 89 // encapsulate the function with the same name as the parent class factory (the return value type is also the same) 90 // </summary> 91 // <returns> </returns> 92 public Operation CreateOperation () 93 {94 // return the above OperationAdd subclass 95 return new OperationSub (); 96} 97} 98 // 7. the Main function calls 99 class Program100 {101 static void Main (string [] args) 102 {103 // to perform addition operations and specify the corresponding factory type, you do not need to enter the Operation type 104 IFactory I = new FactorySub (); 105 // perform the subtraction Operation 106 // IFactory ii = new FactorySub (); 107 Operation op = I. createOperation (); 108 op. numberA = 10; 109 op. numberB = 30; 110 // call the method in the subclass to obtain the result 111 int result = op. getResule (); 112 Console. writeLine (result); 113 Console. readKey (); 114} 115 // if other operations are still required in subsequent programming, add the corresponding subclass to the subclass, 116 // Add the corresponding factory class to the factory method, and you don't need to change the code. This is the 117} 118} of the "profit" of the factory method (abstract factory}