简单工程加反射
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ClassLibrary1.MyTest 7 { 8 /// <summary> 9 /// 运算类 10 /// </summary> 11 public abstract class Operation 12 { 13 /// <summary> 14 /// 初始化属性 15 /// </summary> 16 /// <param name="num1"></param> 17 /// <param name="num2"></param> 18 public Operation(int num1, int num2) 19 { 20 this.Num1 = num1; 21 this.NUm2 = num2; 22 } 23 ///数字1 24 protected int Num1 { get; set; } 25 ///数字2 26 protected int NUm2 { get; set; } 27 /// <summary> 28 /// 计算方法(抽象) 29 /// </summary> 30 /// <returns></returns> 31 public abstract double GetResult(); 32 } 33 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ClassLibrary1.MyTest 7 { 8 /// <summary> 9 /// 除法 10 /// </summary> 11 internal class Division : Operation 12 { 13 public Division(int num1, int num2) : base(num1, num2) { } 14 public override double GetResult() 15 { 16 if (NUm2 == 0) 17 { 18 Console.WriteLine("被除数不能为0"); 19 return 0; 20 } 21 else 22 return Num1 / NUm2; 23 } 24 } 25 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ClassLibrary1.MyTest 7 { 8 /// <summary> 9 /// 加法 10 /// </summary> 11 internal class Plus : Operation 12 { 13 public Plus(int num1, int num2) : base(num1, num2) { } 14 public override double GetResult() 15 { 16 return Num1 + NUm2; 17 } 18 } 19 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ClassLibrary1.MyTest 7 { 8 /// <summary> 9 /// 减法 10 /// </summary> 11 internal class Reduction : Operation 12 { 13 public Reduction(int num1, int num2) 14 : base(num1, num2) { } 15 public override double GetResult() 16 { 17 return Num1 - NUm2; 18 } 19 } 20 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ClassLibrary1.MyTest 7 { 8 /// <summary> 9 /// 简单工厂类 10 /// </summary> 11 public class SimpleFactory 12 { 13 /// <summary> 14 /// 获取需要实例化的类名 15 /// </summary> 16 /// <param name="operationStr"></param> 17 /// <returns></returns> 18 public string GetClassName(string operationStr) 19 { 20 string className = string.Empty; 21 switch (operationStr) 22 { 23 case "+": 24 className = "Plus"; 25 break; 26 case "-": 27 className = "Reduction"; 28 break; 29 case "*": 30 className = "Multiplication"; 31 break; 32 case "/": 33 className = "Division"; 34 break; 35 default: 36 break; 37 } 38 return className; 39 } 40 } 41 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public static void Main() 2 { 3 try 4 { 5 string choose = "yes"; 6 while (choose.ToLower().Equals("yes")) 7 { 8 DoFunction(); 9 Console.WriteLine("\n是否继续测试:Yes or No ?"); 10 choose = Console.ReadLine(); 11 } 12 Console.Read(); 13 } 14 catch (FormatException) 15 { 16 Console.WriteLine("输入字符串格式不争确"); 17 } 18 catch (NullReferenceException) 19 { 20 Console.WriteLine("暂不支持该运算符的运算"); 21 } 22 catch (Exception ex) 23 { 24 Console.WriteLine(ex.InnerException.Message); 25 } 26 } 27 private static void DoFunction() 28 { 29 Console.Write("请输入第一个数:"); 30 int num1 = int.Parse(Console.ReadLine()); 31 Console.Write("请输入第二个数:"); 32 int num2 = int.Parse(Console.ReadLine()); 33 Console.Write("请输入运算符:"); 34 string operationStr = Console.ReadLine(); 35 SimpleFactory simpleFactory = new SimpleFactory(); 36 object[] param = new object[2]; 37 param[0] = num1; 38 param[1] = num2; 39 Operation obj = (Operation)Assembly.Load("ClassLibrary1").CreateInstance("ClassLibrary1.MyTest." + 40 simpleFactory.GetClassName(operationStr), false, BindingFlags.CreateInstance, null, param, null, null); 41 42 Console.WriteLine(string.Format("运算结果:{0}", obj.GetResult())); 43 }
总结:
这里主要涉及几个类,分别是简单工厂(SimpleFactory)、运算类(Operation)和实现类(Plus,Reduction、Division等)。在这里我们依据面向对象的基本准则(单一职责、开发-封闭、依赖倒置、里氏替换),可以设计各执其责的几个实现类来实现同一个抽象方法,达到一个类只做一件事,如果新增运算符类可以扩展,不会做修改,同事也满足了抽象不依赖于具体,而具体应该依赖于抽象,又因为运算符号类(加、减、乘、除)实现了运算类(Operation),从而引申出里氏替换原则。可以看看下UML图:
如果后续有其他的类,我们可以再添加运算符类,实现Operation的GetResult方法就可以了.