static void Main(string[] args) { Operation oper; oper = OperationFactory.CreateOperate("+"); oper.NumberA = 1; oper.NumberB = 2; Console.WriteLine(oper.GetResult()); oper = OperationFactory.CreateOperate("-"); oper.NumberA = 1; oper.NumberB = 2; Console.WriteLine(oper.GetResult()); oper = OperationFactory.CreateOperate("*"); oper.NumberA = 1; oper.NumberB = 2; Console.WriteLine(oper.GetResult()); try { oper = OperationFactory.CreateOperate("/"); oper.NumberA = 1; oper.NumberB = 0; Console.WriteLine(oper.GetResult()); } catch (Exception e) { Console.WriteLine("异常:" + e.Message); } Console.ReadLine(); }
static void Main(string[] args) { try { Operation oper; Console.WriteLine("请输入数字A:"); string strNumberA = Console.ReadLine(); Console.WriteLine("请输入运算符号(+、-、*、/):"); string strOperate = Console.ReadLine(); oper = OperationFactory.createOperate(strOperate); Console.WriteLine("请输入数字B:"); string strNumberB = Console.ReadLine(); oper.NumberA = Convert.ToDouble(strNumberA); oper.NumberB = Convert.ToDouble(strNumberB); double strResult = oper.GetResult(); Console.WriteLine("结果是:" + strResult); Console.ReadKey(); } catch (Exception ex) { throw new Exception(ex.Message); } }
public static void Main(string[] args) { var oper = OperationFactory.CreateOperation("+"); oper.NumberA = 2.1; oper.NumberB = 1.2; Console.WriteLine($"Opertaion: {oper.NumberA} + {oper.NumberB}, result:{oper.GetResult()}"); Console.ReadLine(); }
private static void Main(string[] args) { var operation = OperationFactory.CreateOperation("*"); operation.numberA = 10; operation.numberB = 20; var result = operation.GetOperstionResult(); Console.WriteLine("运算的结果为:" + result); Console.ReadLine(); }
static void Main(string[] args) { Operation oper; oper = OperationFactory.creatOperation("+"); oper.NumberA = 12; oper.NumberB = 1; double result = oper.GetResult(); Console.WriteLine(result); Console.ReadKey(); }
static void Main(string[] args) { Operation operation = OperationFactory.CreateOperation("-"); operation.NumberA = 1; operation.NumberB = 2; var result = operation.Calculate(); System.Console.WriteLine(result); System.Console.ReadKey(); }
static void Main(string[] args) { //Console.Write("输入A:"); //double numberA = Convert.ToDouble(Console.ReadLine()); //Console.Write("选择运算符(+、-、*、/、) :"); //string strOper = Console.ReadLine(); //Console.Write("输入B :"); //double numberB = Convert.ToDouble(Console.ReadLine()); Operation oper = OperationFactory.CreateOperate("+"); oper.NumberA = 1; oper.NumberB = 2; double result = oper.GetResult(); //Console.ReadLine(); }
static void Main(string[] args) { //simple factory IProduct product = Factory.Create(); product.Display(); //Example as below Operation operation = OperationFactory.CreateOperate("+"); operation.NumberA = 10; operation.NumberB = 5; Console.WriteLine(operation.GetResult()); Console.ReadKey(); }
static void Main(string[] args) { Operation oper; oper = OperationFactory.CreateOperate("+"); oper.NumberA = 1; oper.NumberB = 2; double result = oper.GetRestult(); Console.WriteLine(result); //策略模式 Context context; context = new Context(new ContreteStrategyA()); context.ContextInterface(); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("请输入第一个数字:"); var strNunmberA = Console.ReadLine(); Console.WriteLine("请选择运算符(+、-、*、、):"); var strOperate = Console.ReadLine(); Console.WriteLine("请输入第二个数字:"); var strNunmberB = Console.ReadLine(); var oper = OperationFactory.CreaOperation(strOperate); oper.NumberA = Convert.ToDouble(strNunmberA); oper.NumberB = Convert.ToDouble(strNunmberB); var result = oper.GetResult(); Console.WriteLine(result); Console.ReadLine(); }
static void Main(string[] args) { //1.抽象类实现 var product = Structural.Factory.CreateProduct(); Console.WriteLine("Created {0}", product.GetType().Name); //2.接口实现 var product2 = StructuralInterfaceImplementation.Factory.Create(); product2.Display(); //3.计算器练习 Operation operateion = OperationFactory.CreateOperate("+"); operateion.NumberA = 10; operateion.NumberB = 5; Console.WriteLine(operateion.GetResult()); //4.银行支付练习 var orderService = new OrderService(); orderService.CreateOrder("ICBC"); Console.ReadLine(); /* * * 5.1 简单工厂模式优点 * ◊ 实现创建和使用分离; * ◊ Client无需知道所创建的ConcreteProduct类名,只需要知道ConcreteProduct所对应的参数。 * 5.2 简单工厂模式缺点 * ◊ Factory类集中所有ConcreteProduct的创建逻辑,职责过重。一旦需要添加新的ConcreteProduct,则需要修改Factory逻辑。这样违背了OCP(开放-关闭原则)。 * ◊ 由于使用了static方法,造成Factory无法形成基于继承的结构。 * */ }