1.大话设计模式-简单工厂模式

编程时一门技术,更是一门艺术

简单工厂模式利用面向对象方式通过继承、封装、多态把程序的耦合度降低,设计模式使得程序更加灵活,容易修改,易于复用。

下面是服务器计算器代码:

 1 using System;

 2 using System.Collections.Generic;

 3 using System.Linq;

 4 using System.Text;

 5 using System.Threading.Tasks;

 6 

 7 namespace DesignModel

 8 {

 9     /// <summary>

10     /// 计算器

11     /// </summary>

12     public class Calculator   //创建一个计算器的基类可以接受两个参数,任何算法只需重写计算结果方法即可。

13     {

14         private double _numberA;

15         private double _numberB;

16 

17         public double NumberA

18         {

19             get { return this._numberA; }

20             set { this._numberA = value; }

21         }

22         public double NumberB

23         {

24             get { return this._numberB; }

25             set { this._numberB = value; }

26         }

27         public virtual double GetResult()

28         {

29             double result = 0;

30             return result;

31         }

32     }

33     /// <summary>

34     /// 加法

35     /// </summary>

36     public class Add : Calculator    //每添加一种计算方式只需添加一个计算类并重写基类方法即可

37     {

38         public override double GetResult()

39         {

40             return  NumberA + NumberB;

41         }

42     }

43     /// <summary>

44     /// 减法

45     /// </summary>

46     public class Sub : Calculator

47     {

48         public override double GetResult()

49         {

50             return NumberA + NumberB;

51         }

52     }

53 

54     /// <summary>

55     /// 计算器工厂

56     /// </summary>

57     public class CalculatorFactory

58     {

59         public static Calculator GetResult(string oper)

60         {

61             Calculator calcu = null;

62             switch (oper)

63             {

64                 case "+":

65                     calcu = new Add();

66                     break;

67                 case "-":

68                     calcu = new Sub();

69                     break;

70             }

71             return calcu;

72         }

73 

74     }

75 

76 

77 }
计算器类
 1  static void Main(string[] args)

 2         {

 3             Console.WriteLine("请输入数字A:");

 4             string numbera = Console.ReadLine();

 5             Console.WriteLine("请输入运算符:");

 6             string oper = Console.ReadLine();

 7             Console.WriteLine("请输入数字B:");

 8             string numberb = Console.ReadLine();

 9 

10             Calculator c = CalculatorFactory.GetResult(oper);

11             c.NumberA = Convert.ToDouble(numbera);

12             c.NumberB = Convert.ToDouble(numberb);

13             Console.WriteLine(string.Format("{0}{1}{2}={3}", numbera, oper, numberb, c.GetResult()));

14             Console.ReadLine();

15         }
客户端调用

基本验证没加,学习练习的同学可以自己加上

28种设计模式后续更新

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignModel
{
    /// <summary>
    /// 计算器
    /// </summary>
    public class Calculator   //创建一个计算器的基类可以接受两个参数,任何算法只需重写计算结果方法即可。
    {
        private double _numberA;
        private double _numberB;

        public double NumberA
        {
            get { return this._numberA; }
            set { this._numberA = value; }
        }
        public double NumberB
        {
            get { return this._numberB; }
            set { this._numberB = value; }
        }
        public virtual double GetResult()
        {
            double result = 0;
            return result;
        }
    }
    /// <summary>
    /// 加法
    /// </summary>
    public class Add : Calculator    //每添加一种计算方式只需添加一个计算类并重写基类方法即可
    {
        public override double GetResult()
        {
            return  NumberA + NumberB;
        }
    }
    /// <summary>
    /// 减法
    /// </summary>
    public class Sub : Calculator
    {
        public override double GetResult()
        {
            return NumberA + NumberB;
        }
    }

    /// <summary>
    /// 计算器工厂
    /// </summary>
    public class CalculatorFactory
    {
        public static Calculator GetResult(string oper)
        {
            Calculator calcu = null;
            switch (oper)
            {
                case "+":
                    calcu = new Add();
                    break;
                case "-":
                    calcu = new Sub();
                    break;
            }
            return calcu;
        }

    }


}

你可能感兴趣的:(简单工厂模式)