• 设计模式学习笔记——抽象工厂(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
  • 相关阅读:
    ARC 基础(上)
    将字符串写到屏幕上
    UIColor 详解
    the complexity is no longer O(lgn), right?
    [LeetCode] Sum Root to Leaf Numbers, Solution
    [LeetCode] Word Ladder II, Solution
    [Microsoft] Intealeaving of two given strings, Solution
    [Yahoo] Cloest palindrome number, Solution
    [LeetCode] Longest Consecutive Sequence, Solution
    Summary
  • 原文地址:https://www.cnblogs.com/chuifeng/p/1916599.html
Copyright © 2020-2023  润新知