• 抽象工厂模式(Abstract Factory Pattern)


    动机(Motivation)

    在软件系统中,经常面临着“一系列相互依赖的对象”的创建工作;同时,由于需求的变化,往往存在更多系列对象的创建工作。
    如何应对这种变化?如何绕过常规的对象创建方法(new),提供一种“封装机制”来避免客户程序和这种“多系列具体对象创建工作”的紧耦合?

    意图(Intent)

    提供一个接口,让该接口负责创建一系列“相关或者相互依赖的对象”,无需指定它们具体的类。

    Abstract Factory模式的几个要点

    (1) 如果没有应对“多系列对象构建”的需求变化,则没有必要使用Abstract Factory模式,这时候使用简单的静态工厂完全可以。
    (2)“系列对象”指的是这些对象之间有相互依赖、或作用的关系,例如游戏开发场景中的“道路”与“房屋”的依赖,“道路”与“地道”的依赖。
    (3) Abstract Factory模式主要在于应对“新系列”的需求变动。其缺点在于难以应对“新对象”的需求变动。
    (4) Abstract Factory模式经常和Factory Method模式共同组合来应对“对象创建”的需求变化。

      1 namespace 抽象工厂
      2 {
      3     public interface IHuman
      4     {
      5         void GetColor();
      6         void GetGender();
      7     }
      8 }
      9 
     10 namespace 抽象工厂
     11 {
     12     public abstract class MaleHuman : IHuman
     13     {
     14         public abstract void GetColor();
     15 
     16         public void GetGender()
     17         {            
     18             Console.WriteLine("男人就是累啊!");
     19         }
     20     }
     21 }
     22 
     23 namespace 抽象工厂
     24 {
     25     public abstract class FemaleHuman:IHuman
     26     {
     27         public abstract void GetColor();
     28 
     29         public void GetGender()
     30         {
     31             Console.WriteLine("女人也顶半边天!");
     32         }
     33     }
     34 }
     35 
     36 namespace 抽象工厂
     37 {
     38     public class WhiteMaleHuman : MaleHuman
     39     {
     40         public override void GetColor()
     41         {
     42             Console.WriteLine("我是白人,我真白");
     43         }
     44     }
     45 }
     46 
     47 namespace 抽象工厂
     48 {
     49     public class WhiteFemaleHuman : FemaleHuman
     50     {
     51         public override void GetColor()
     52         {
     53             Console.WriteLine("我是白人,我真白!");
     54         }
     55     }
     56 }
     57 
     58 namespace 抽象工厂
     59 {
     60     public class BlackMaleHuman : MaleHuman
     61     {
     62         public override void GetColor()
     63         {
     64             Console.WriteLine("我是黑人,我真壮!");
     65         }
     66     }
     67 }
     68 
     69 namespace 抽象工厂
     70 {
     71     public class BlackFemaleHuman : FemaleHuman
     72     {
     73         public override void GetColor()
     74         {
     75             Console.WriteLine("我是黑人,我真壮!");
     76         }
     77     }
     78 }
     79 
     80 namespace 抽象工厂
     81 {
     82     public class YellowMaleHuman : MaleHuman
     83     {
     84         public override void GetColor()
     85         {
     86             Console.WriteLine("我是黄种人,我骄傲!");
     87         }
     88     }
     89 }
     90 
     91 namespace 抽象工厂
     92 {
     93     public class YellowFemaleHuman : FemaleHuman
     94     {
     95         public override void GetColor()
     96         {
     97             Console.WriteLine("我是黄种人,我骄傲!");
     98         }
     99     }
    100 }
    101 
    102 namespace 抽象工厂
    103 {
    104     interface IFactory
    105     {
    106         IHuman CreateMaleHuman();
    107         IHuman CreateFemaleHuman();
    108     }
    109 }
    110 
    111 namespace 抽象工厂
    112 {
    113     public class WhiteHumanFactory:IFactory
    114     {
    115         public IHuman CreateMaleHuman()
    116         {
    117             return new WhiteMaleHuman();
    118         }
    119 
    120         public IHuman CreateFemaleHuman()
    121         {
    122             return new WhiteFemaleHuman();
    123         }
    124     }
    125 }
    126 
    127 namespace 抽象工厂
    128 {
    129     public class BlackHumanFactory : IFactory
    130     {
    131         public IHuman CreateMaleHuman()
    132         {
    133             return new BlackMaleHuman();
    134         }
    135 
    136         public IHuman CreateFemaleHuman()
    137         {
    138             return new BlackMaleHuman();
    139         }
    140     }
    141 }
    142 
    143 namespace 抽象工厂
    144 {
    145     public class YellowHumanFactory : IFactory
    146     {
    147         public IHuman CreateMaleHuman()
    148         {
    149             return new YellowMaleHuman();
    150         }
    151 
    152         public IHuman CreateFemaleHuman()
    153         {
    154             return new YellowMaleHuman();
    155         }
    156     }
    157 }
    158 
    159 namespace 抽象工厂
    160 {
    161     class Program
    162     {
    163         static void Main(string[] args)
    164         {
    165             IFactory whiteHumanFactory = new WhiteHumanFactory();
    166             IHuman whiteMaleHuman = whiteHumanFactory.CreateMaleHuman();
    167             whiteMaleHuman.GetColor();
    168             whiteMaleHuman.GetGender();
    169 
    170             IHuman whiteFemaleHuman = whiteHumanFactory.CreateFemaleHuman();
    171             whiteFemaleHuman.GetColor();
    172             whiteFemaleHuman.GetGender();
    173 
    174             IFactory blackHumanFactory = new BlackHumanFactory();
    175             IHuman blackMaleHuman = blackHumanFactory.CreateMaleHuman();
    176             blackMaleHuman.GetColor();
    177             blackMaleHuman.GetGender();
    178 
    179             IHuman blackFemaleHuman = blackHumanFactory.CreateFemaleHuman();
    180             blackFemaleHuman.GetColor();
    181             blackFemaleHuman.GetGender();
    182 
    183             IFactory yellowHumanFactory = new YellowHumanFactory();
    184             IHuman yellowMaleHuman = yellowHumanFactory.CreateMaleHuman();
    185             yellowMaleHuman.GetColor();
    186             yellowMaleHuman.GetGender();
    187 
    188             IHuman yellowFemaleHuman = yellowHumanFactory.CreateFemaleHuman();
    189             yellowFemaleHuman.GetColor();
    190             yellowFemaleHuman.GetGender();
    191 
    192             Console.ReadKey();
    193         }
    194     }
    195 }
  • 相关阅读:
    使用SharePoint PeopleEditor控件
    python快排的三种写法
    js获取浏览器屏幕高度、宽度等
    Spring总结> 第一结 概述
    js设置body高度、宽度为浏览器窗口高度、宽度
    简单的Spring调用jdbc.porperties配置信息,以mysql为例
    SSM(Spring + Spring MVC + MyBatis)整合
    【原创】MS SQL2005 存储过程分页(简洁型)后续完善更新中......
    【转载】动态sql语句基本语法
    Provider模式Demo
  • 原文地址:https://www.cnblogs.com/zhxlsuyu/p/3514031.html
Copyright © 2020-2023  润新知