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

  • 相关阅读:
    Golang 版本 支付宝支付SDK app支付接口2.0
    用 Certbot-auto 在 letsencrypt.org申请免费 SSL 证书实现 HTTPS
    Go类型断言demo
    签名算法
    golang cron定时任务简单实现
    Chapter Zero 0.2.2 内存
    Linux-系统目录结构
    Linux-关于Bash
    Chapter Zero 0.2.1 执行运算与判断的CPU
    Chapter Zero 0.1.4 计算机上常用的计算单位
  • 原文地址:https://www.cnblogs.com/haunge/p/15034915.html
Copyright © 2011-2022 走看看