标签:style blog color io 使用 ar sp div on
简单工厂模式:简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现。
(一)简单工厂模式的特点:
简单工厂模式实质是由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类(这些产品类继承自一个父类或接口)的实例。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 简单工厂模式 7 { 8 public abstract class Operation 9 { 10 private double numberA = 0d; 11 public double NumberA 12 { 13 get { return numberA; } 14 set { numberA = value; } 15 } 16 17 private double numberB = 0d; 18 public double NumberB 19 { 20 get { return numberB; } 21 set { numberB = value; } 22 } 23 24 public abstract double GetResult(); 25 } 26 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 简单工厂模式 7 { 8 class OperationAdd:Operation 9 { 10 public override double GetResult() 11 { 12 return NumberA + NumberB; 13 } 14 } 15 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 简单工厂模式 7 { 8 class OperationSub:Operation 9 { 10 public override double GetResult() 11 { 12 return NumberA - NumberB; 13 } 14 } 15 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 简单工厂模式 7 { 8 class OperationMul:Operation 9 { 10 public override double GetResult() 11 { 12 return NumberA * NumberB; 13 } 14 } 15 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 简单工厂模式 7 { 8 class OperationDiv:Operation 9 { 10 public override double GetResult() 11 { 12 if (NumberB != 0) 13 { 14 return NumberA / NumberB; 15 } 16 else 17 { 18 throw new Exception("除数不能为零"); 19 } 20 } 21 } 22 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 简单工厂模式 7 { 8 /// <summary> 9 /// 通过工厂,只要输入运算符号,就可以实例化出正确的对象 10 /// </summary> 11 class OperationFactory 12 { 13 public Operation GetOperation(string operate) 14 { 15 Operation operation = null; 16 switch (operate) 17 { 18 case "+": 19 operation = new OperationAdd(); 20 break; 21 case "-": 22 operation = new OperationSub(); 23 break; 24 case "*": 25 operation = new OperationMul(); 26 break; 27 case "/": 28 operation = new OperationDiv(); 29 break; 30 } 31 return operation; 32 } 33 } 34 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 简单工厂模式 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 while (true) 13 { 14 try 15 { 16 Console.WriteLine("输入第一个数:"); 17 double numberA = Convert.ToDouble(Console.ReadLine()); 18 Console.WriteLine("输入运算符号:"); 19 string operate = Console.ReadLine(); 20 Console.WriteLine("输入第二个数:"); 21 double numberB = Convert.ToDouble(Console.ReadLine()); 22 OperationFactory factory = new OperationFactory(); 23 Operation operation = factory.GetOperation(operate); 24 operation.NumberA = numberA; 25 operation.NumberB = numberB; 26 Console.WriteLine("结果是:" + operation.GetResult()); 27 } 28 catch(Exception e) 29 { 30 Console.WriteLine(e.Message); 31 Console.ReadKey(); 32 Console.Clear(); 33 continue; 34 } 35 } 36 } 37 } 38 }
标签:style blog color io 使用 ar sp div on
原文地址:http://www.cnblogs.com/runonroad/p/4022217.html