zoukankan      html  css  js  c++  java
  • 创建型设计模式之-工厂方法(这个应该不算设计模式的一种)

    先看代码:

     1     //工程方法
     2     public class Create
     3     {
     4         static void Main()
     5         {
     6             IBuySome buySome = new FactoryBuy().GetBuy(ClassType.ByFruits);
     7         }
     8     }
     9     public interface IBuySome
    10     {
    11         void Buy();
    12     }
    13 
    14     public class ByPen : IBuySome
    15     {
    16         public void Buy()
    17         {
    18             Console.WriteLine("买笔");
    19         }
    20     }
    21     public class ByFruits : IBuySome
    22     {
    23         public void Buy()
    24         {
    25             Console.WriteLine("水果");
    26         }
    27     }
    28     public class BySugar : IBuySome
    29     {
    30         public void Buy()
    31         {
    32             Console.WriteLine("买糖果");
    33         }
    34     }
    35 
    36     public class FactoryBuy 
    37     {
    38         public IBuySome GetBuy(ClassType classType)
    39         {
    40             switch (classType)
    41             {
    42                 case ClassType.ByFruits:
    43                     return new ByFruits();
    44                 case ClassType.ByPen:
    45                     return new ByPen();
    46                 case ClassType.BySugar:
    47                     return new BySugar();
    48                 default:
    49                     throw new Exception("没有这个枚举!");
    50             }
    51         }
    52     }
    53     public enum ClassType
    54     {
    55         ByFruits,
    56         BySugar,
    57         ByPen
    58     }
    View Code

    这个模式依赖抽象!也就是一个共同的接口。为什么在写一个工厂来去实例化呢,那不是违反了低耦合原则了吗?是违反了低耦合的原则,但是满足了扩展需求。每个设计模式都不是完美的,都是解决一类问题,但是解决一类问题也会引发其他问题。为了解决这个耦合问题。就可以(反射+配置文件)

     1 //工厂模式
     2     public class Create
     3     {
     4         static void Main()
     5         {
     6             IBuySome buySome= new FactoryBuy().GetBuy();
     7             //这样扩展就很方便。有新功能添加,直接实现原有接口,然后该下配置文件。就不需要修改源码。
     8             //就很好的解决了上面工厂方法的问题
     9         }
    10     }
    11     public interface IBuySome
    12     {
    13         void Buy();
    14     }
    15 
    16     public class ByPen : IBuySome
    17     {
    18         public void Buy()
    19         {
    20             Console.WriteLine("买笔");
    21         }
    22     }
    23     public class ByFruits : IBuySome
    24     {
    25         public void Buy()
    26         {
    27             Console.WriteLine("水果");
    28         }
    29     }
    30     public class BySugar : IBuySome
    31     {
    32         public void Buy()
    33         {
    34             Console.WriteLine("买糖果");
    35         }
    36     }
    37 
    38     public class FactoryBuy 
    39     {
    40         public IBuySome GetBuy()
    41         {
    42             ///获取dll的地址 比如:"C:Usersguoyzsource
    eposHYC_C_CeShiHYC_C_CeShi_ExtendCoreinDebugCeShi_ExtendCore.dll"
    43             var StrDllPath = ConfigurationManager.AppSettings["path"];
    44             //获取类型  比如:ByPen 就是这个
    45             var StrType = ConfigurationManager.AppSettings["Type"];
    46             Assembly assembly = Assembly.Load(StrDllPath);
    47             //获取具体类型
    48             Type type = assembly.GetType(StrType, false, true);
    49             //获取类型
    50             return (IBuySome)Activator.CreateInstance(type);
    51         }
    52     }
    View Code

    本文来自博客园,作者:小换哥,转载请注明原文链接:https://www.cnblogs.com/haunge/p/15034915.html

  • 相关阅读:
    18 | 案例篇:内存泄漏了,我该如何定位和处理?
    17 | 案例篇:如何利用系统缓存优化程序的运行效率?
    16 | 基础篇:怎么理解内存中的Buffer和Cache?
    Scrapyd 改进第一步: Web Interface 添加 charset=UTF-8, 避免查看 log 出现中文乱码
    scrapy_redis 相关: 将 jobdir 保存的爬虫进度转移到 Redis
    lxml.etree.HTML(text) 解析HTML文档
    CSS/Xpath 选择器 第几个子节点/父节点/兄弟节点
    scrapy_redis 相关: 查看保存的数据
    scrapy 通过FormRequest模拟登录再继续
    python2 python3 转换,兼容
  • 原文地址:https://www.cnblogs.com/haunge/p/15034915.html
Copyright © 2011-2022 走看看