设计模式-工厂模式

一、简单工厂模式
实现效果
static void Main(string[] args)
        {

            //非工厂模式
            //OperationAdd add = new OperationAdd();
            //add.NumberA = 2;
            //add.NumberB = 3;
            //Console.WriteLine(add.GetResult());

            //简单工厂模式
            Operation ope = OperationFactory.creatOperate("+");
            ope.NumberA = 5;
            ope.NumberB = 2;
            Console.WriteLine(ope.GetResult());

        }
 
实现过程:
(1)创建Operation抽象类
public abstract class Operation
    {
        public double NumberA;
        public double NumberB;
        public abstract double GetResult();
    }
(2)分别用OperationAdd、OperationSub、OperationMul、OperationDiv类继承Operation抽象类
public class OperationAdd : Operation
    {
        public override double GetResult()
        {
            return NumberA + NumberB;
        }
    }
 
public class OperationSub : Operation
    {
        public override double GetResult()
        {
            return NumberA - NumberB;
        }
    }
 
  public class OperationMul : Operation
    {
        public override double GetResult()
        {
            return NumberA * NumberB;
        }
    }
 
public class OperationDiv : Operation
    {
        public override double GetResult()
        {
            if (NumberB==0)
            {
                return 0;
            }
            return NumberA / NumberB;
        }
    }
 
(3)创建工厂类OperationFactory
public  class OperationFactory
    {
        public static Operation creatOperate(string operate)
        {
            Operation ope=null;
            switch (operate)
            {
                case "+":
                    ope= new OperationAdd();
                    break;
                case "-":
                    ope = new OperationSub();
                    break;
                case "*":
                    ope = new OperationMul();
                    break;
                case "/":
                    ope = new OperationDiv();
                    break;
            }
            return ope;
        }
    }
 
二、工厂方法模式
工厂方法模式:定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。
  static void Main(string[] args)
        {

            //非工厂模式
            //OperationAdd add = new OperationAdd();
            //add.NumberA = 2;
            //add.NumberB = 3;
            //Console.WriteLine(add.GetResult());

            //简单工厂模式
            //Operation ope = OperationFactory.creatOperate("+");
            //ope.NumberA = 5;
            //ope.NumberB = 2;
            //Console.WriteLine(ope.GetResult());

            //工厂方法模式
            IFactory operFactory = new AddFactory();
            Operation oper = operFactory.CreatOeration();
            oper.NumberA = 5;
            oper.NumberB = 6;
            Console.WriteLine(oper.GetResult());
        }
 
实现过程
(1)原来的OperationAdd、OperationSub、OperationMul、OperationDiv类不变
(2)添加接口IFactory,以及AddFactory、SubFactory、MulFactory和DivFactory类实现IFactory,分别在类的内部创建加减乘除操作的实例。
interface IFactory
    {
        Operation CreatOeration();
    }
class AddFactory : IFactory
    {
        public Operation CreatOeration()
        {
            return new OperationAdd();
        }
    }
 
class SubFactory : IFactory
    {
        public Operation CreatOeration()
        {
            return new OperationSub();
        }
    }
 
class MulFactory : IFactory
    {
        public Operation CreatOeration()
        {
            return new OperationMul();
        }
    }
 
class DivFactory : IFactory
    {
        public Operation CreatOeration()
        {
            return new OperationDiv();
        }
    }
 

posted @ 2020-04-19 23:23  我瘦够了  阅读(161)  评论(0编辑  收藏  举报