zoukankan      html  css  js  c++  java
  • 设计模式学习 之 简单工厂模式

    有幸看到园友推荐的 《大话设计模式》十分感谢,准备练习一下,以免忘记。

    设计模式 需要灵活运用,不能为了 设计而设计,这样就得不偿失了。

     23种设计模式 分为 三大类型

    创建型模式:单例模式、抽象工厂模式、建造者模式、工厂模式、原型模式。

    结构型模式:适配器模式、桥接模式、装饰模式、组合模式、外观模式、享元模式、代理模式。

    行为型模式:模版方法模式、命令模式、迭代器模式、观察者模式、中介者模式、备忘录模式、解释器模式(Interpreter模式)、状态模式、策略模式、职责链模式(责任链模式)、访问者模式。

    简单工厂模式 又叫做 静态方法 模式 ,不在 23 种 设计模式之内 。是项目中最简单实用的 模式。它避免了大量的 代码 写在 一起 造成的 项目混乱,实现了简单的分层。

    可以使用户只关心 使用 ,传入参数 调用方法。 缺点 是 大量的 业务 逻辑 处理 写在同一个 工厂 内, 当 需求发生 变动时,需要去修改工厂内的方法,这样违反了开放封闭原则。

    写个例子:输入 一个数字 ,根据 数字 大小 对它 进行 ,一倍, 两倍  ,3倍  , 加 3 的 操作 ;

    前台代码

     1 public  static void Main(string[] args)
     2         {
     3 
     4             FactortMethod factory = new FactortMethod();
     5          
     6             for (int i = 0; i < 5; i++)
     7             {
     8                 int num = Convert.ToInt32(Console.ReadLine());
     9 
    10                 Operation oper;
    11 
    12                 oper = factory.Creat(num);
    13 
    14                 oper.InputNum = num;
    15 
    16                 Console.WriteLine(oper.OpenSoftWare());
    17              
    18                                        
    19             }
    20 
    21         }

    如果我知道了 调用的功能,可以不用 关心 调用方法是谁写的,怎么实现的,只需  选择 我要使用的 方法 ,赋值

    公共操作类

     1 public class Operation 
     2     {
     3         private int inputNum;
     4         public int InputNum
     5         {
     6             get
     7             {
     8                 return inputNum;
     9             }
    10             set
    11             {
    12                 inputNum = value;
    13 
    14             }
    15         }
    16         public virtual int OpenSoftWare()
    17         {
    18 
    19             int Select = 0;
    20             return Select;
    21 
    22         }
    23     }
    View Code

    具体 操作类 继承 公共类

      public class FactoryOperationOrigin : Operation
            {
                public override int OpenSoftWare()
                {
    
                    int Times = InputNum;
                     return Times;
                }
    
            }
    
            public class FactoryOperationTwo_Times : Operation
            {
    
                public override int OpenSoftWare()
                {
                    int Times = InputNum * 2;
                    return Times;
    
                }
    
            }
    
            public class FactoryOperationThere_Times : Operation
            {
                public override int OpenSoftWare()
                        {
                            int Times = InputNum * 3;
                                        return Times;
                        }
            }
    
            public class FactoryOperationAddThere : Operation
            {
                public override int OpenSoftWare()
                {
                    int Times = InputNum + 3;
                    return Times;
                }
    
    
            }
    View Code

    工厂 方法 进行 逻辑处理 ,选择 相应的 操作子类

     public class  FactortMethod 
        {
        
          public  Operation  Creat (int  num)
    {
    
         if(num < 10)
         {
    
             return new FactoryOperationOrigin();
         
         }
    
         else 
             
             if(num > 10 && num < 30)
         {
           return new  FactoryOperationTwo_Times();
         
         }
    
         else
             if(num > 30  && num < 60)
         {
           return new FactoryOperationThere_Times();
         
         }
    
             else
    
         {
             return new FactoryOperationAddThere();
           
         }
              
          }
    }
        
        
        
        }
    View Code

    遇到的问题: 注意虚方法  父类 使用 virtual   可以让任何派生类 改写 函数   派生类 重写父类方法 要 使用 override 

  • 相关阅读:
    龟兔赛跑(动态规划)
    Dividing (多重背包 搜索)
    第k大值01背包问题
    FATE(完全背包)
    01背包 和 完全背包 详解
    放苹果(动态规划)
    max Sum(简单动态规划)
    连连看 优先对列 应用2
    尺取法
    Square(强大的剪枝)
  • 原文地址:https://www.cnblogs.com/lfyy/p/5084381.html
Copyright © 2011-2022 走看看