• 设计模式学习笔记——抽象工厂(Abstract Factory)


    学习TerryLee的设计模式颇有感触,留下以下笔记以作日后参考。

    代码
    //-----------------------------------------------
    //抽象工厂是处理复合对象的创建。
    //和工厂方法相同将类的创建推向了客户端更把创建的逻辑推向了客户端。
    //-----------------------------------------------

    #region 产品

    #region 简单产品

    public abstract class Monitor
    {
    }

    public abstract class Box
    {
    }

    #endregion

    #region 复合产品

    public abstract class PC
    {
    public abstract Monitor CreateMonitor();

    public abstract Box CreateBox();
    }

    public class TypeAPC : PC
    {

    public override Monitor CreateMonitor()
    {
    throw new NotImplementedException();
    }

    public override Box CreateBox()
    {
    throw new NotImplementedException();
    }
    }

    public class TypeBPC : PC
    {
    public override Monitor CreateMonitor()
    {
    throw new NotImplementedException();
    }

    public override Box CreateBox()
    {
    throw new NotImplementedException();
    }
    }

    #endregion

    #endregion

    #region 工厂

    public class PCFactory
    {
    public Monitor pen;

    public Box Box;

    public void CreatPC(PC some)
    {
    pen
    = some.CreateMonitor();
    Box
    = some.CreateBox();
    }
    }

    #endregion

    #region 客户端

    public class App
    {
    public static void Main(string[] args)
    {
    string strfactoryName = "TypeAPC";

    PC pc;

    pc
    = (PC)Assembly.Load("FactoryMethod").CreateInstance("FactoryMethod." + strfactoryName);

    PCFactory factory
    = new PCFactory();

    factory.CreatPC(pc);
    }
    }

    #endregion
  • 相关阅读:
    CISCO DHCP全攻略详解
    CentOS配置远程日志服务器
    CentOS下的日志切割
    CentOS日志的简单介绍
    H3C Telnet 配置
    CentOS7图形界面与命令行界面切换
    思科4506E做ehterchannel故障排查
    EtherChannel Cisco 端口聚合详解
    算法练习2之单链表求和
    Retrofit2的GsonConverterFactory.create()和RxJava2CallAdapterFactory.create()的实现过程以及执行过程
  • 原文地址:https://www.cnblogs.com/chuifeng/p/1916599.html
Copyright © 2020-2023  润新知