标签:
/// <summary> /// 操作基类 /// </summary> public class Operation { private double _numberA = 0; private double _numberB = 0; public double NumberA { get { return _numberA; } set { _numberA = value; } } public double NumberB { get { return _numberB; } set { _numberB = value; } } public virtual double GetResult() { return 0; } } /// <summary> /// 具体操作类 /// </summary> public class OperationAdd : Operation { public override double GetResult() { double result = 0; result = NumberA + NumberB; return result; } } public class OperationSub : Operation { public override double GetResult() { double result = 0; result = NumberA + NumberB; return result; } } /// <summary> /// 工厂根据需求生产某一类型的事物 /// </summary> public class OperationFactory { public static Operation createOperation(string operate) { Operation func = null; switch(operate) { case "+": func = new OperationAdd(); break; case "-": func = new OperationSub();; break; } return func; } } class Program { static void Main(string[] args) { var operation = OperationFactory.createOperation("+"); operation.NumberA = 10; operation.NumberB = 5; operation.GetResult(); } }
标签:
原文地址:http://www.cnblogs.com/Daniel-Liang/p/5851415.html