• 设计模式-工厂模式


    一、简单工厂模式
    实现效果
    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();
            }
        }
     

  • 相关阅读:
    SQL中Group By的使用
    SQL Server中的Merge关键字
    C# 9.0 正式发布了(C# 9.0 on the record)
    sql server 本地复制订阅 实现数据库服务器 读写分离
    SQL Server游标
    走进异步编程的世界 开始接触 async/await
    C#发展历程以及C#6.0新特性
    .NET NPOI导出Excel详解
    Centos 7搭建Gitlab服务器超详细
    使用python设置word中的字体样式
  • 原文地址:https://www.cnblogs.com/shougoule/p/12735012.html
Copyright © 2020-2023  润新知