C # simple factory with design patterns,
1. The three attributes of object-oriented, encapsulation, inheritance, and polymorphism, take a single computer as an example:
Creating a parent class Operation has two attributes and a calculation method (virtual method) to facilitate subclass Rewriting:
1 public class Operation 2 {3 private double _ numberA = 0; 4 private double _ numberB = 0; 5 6 public double NumberA 7 {8 get {return _ numberA ;} 9 set {_ numberA = value;} 10} 11 12 public double NumberB 13 {14 get {return _ numberB;} 15 set {_ numberB = value ;} 16} 17 18 /// <summary> virtual method 19 /// 20 /// </summary> 21 /// <returns> </returns> 22 public virtual double GetResult () 23 {24 double result = 0; 25 return result; 26} 27}View Code
Next, create a computing class, add, subtract, multiply, and divide the class to integrate the operation class.
Add class
1 public class OperationAdd: Operation 2 {3 public override double GetResult () 4 {5 double result = 0; 6 result = NumberA + NumberB; 7 return result; 8} 9}View Code
Subtraction
1 public class OperationSub: Operation 2 {3 public override double GetResult () 4 {5 double result = 0; 6 result = NumberA-NumberB; 7 return result; 8} 9}View Code
Multiplication class
1 public class OperationMul: Operation 2 {3 public override double GetResult () 4 {5 double result = 0; 6 result = NumberA * NumberB; 7 return result; 8} 9}View Code
Division class
1 public class OperationDiv: Operation 2 {3 public override double GetResult () 4 {5 double result = 0; 6 if (NumberB = 0) 7 throw new Exception ("the divisor cannot be 0"); 8 result = NumberA/NumberB; 9 return result; 10} 11}View Code
Then, you can use a simple factory class to create a computing instance.
1 public class OperationFactory 2 {3 public static Operation createOperate (string operate) 4 {5 Operation operator = null; 6 switch (operate) 7 {8 case "+ ": 9 keys = new OperationAdd (); 10 break; 11 case "-": 12 keys = new OperationSub (); 13 break; 14 case "*": 15 bytes = new OperationMul (); 16 break; 17 case "/": 18 bytes = new OperationDiv (); 19 break; 20} 21 return comment; 22} 23}View Code
Finally, call the following interface:
1 static void Main (string [] args) 2 {3 Operation; 4 bytes = OperationFactory. createOperate ("+"); 5 bytes. numberA = 1; 6 bytes. numberB = 2; 7 double result = random. getResult (); 8 Console. writeLine (result); 9 Console. readKey (); 10}View Code
Here, the computing process is encapsulated into a method, and each computing class is integrated with the operation class. As a result, since each method overwrites the computing method, the different forms of a method are shown here, if you want to add other methods in the future, you only need to inherit the operation class to rewrite the calculation method. In this way, the original program will not be modified and the decoupling is realized. The advantage of object-oriented language is maintainability, reusable, scalable, and flexible.
The above is a summary of self-study. I hope there is something wrong with it. Please give me some advice. Thank you.