以下为C#代码以下代码用简单工厂模式实现对两个数的操作方法（计算器）namespace 简单工厂模式 { class Program { static void Main(string[] args) { //使用方法 Operation operation = OperationFactory.CreateOperation("+"); operation.param1 = 2; operation.param2 = 4; Console.WriteLine(operation.GetResult()); Console.ReadKey(); } /// /// 操作类的父类 /// public abstract class Operation { public double param1 { get; set; } public double param2 { get; set; } public abstract double GetResult(); } //加法 public class AddOperation:Operation { public override double GetResult() { return param1 + param2; } } //减法 public class MinusOperation:Operation { public override double GetResult() { return param1 - param2; } } //乘法 public class MulOperation:Operation { public override double GetResult() { return param1 * param2; } } //除法 public class DelOperation:Operation { public override double GetResult() { if(param2==0) { throw new Exception("除数不能为0"); } return param1 / param2; } } //工厂类，用来管理所有的操作,并能向外提供访问方法 public class OperationFactory { //根据操作符去创建对应的类 public static Operation CreateOperation(string operationName) { Operation operation = null; switch(operationName) { case "+": operation = new AddOperation(); break; case "-": operation = new MinusOperation(); break; case "*": operation = new MulOperation(); break; case "/": operation = new DelOperation(); break; } return operation; } } }}