zoukankan      html  css  js  c++  java
  • 抽象工厂

    名称 Abstract Factory
    结构
    意图 提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
    适用性
    • 一个系统要独立于它的产品的创建、组合和表示时。
    • 一个系统要由多个产品系列中的一个来配置时。
    • 当你要强调一系列相关的产品对象的设计以便进行联合使用时。
    • 当你提供一个产品类库,而只想显示它们的接口而不是实现时。
      1// Abstract Factory
      2
      3// Intent: "Provide an interface for creating families of related or
      4// dependent objects without specifying their concrete classes". 
      5
      6// For further information, read "Design Patterns", p87, Gamma et al.,
      7// Addison-Wesley, ISBN:0-201-63361-2
      8
      9/* Notes:
     10 * When the construction needed involves many objects, possible organised
     11 * in multi-faceted arrangements, the entire construction can be delegated
     12 * to an abstract factory. This exposes standardised creation functionality
     13 * which can be customised in concrete implementation to suit your specific
     14 * needs, and avoid embedding this information in higher level code - it
     15 * just needs to know how to call the abstract factory.
     16 * 
     17 * In this sample, we have a framework with three abstract operating classes, 
     18 * called DPDocument, DPWorkspace and DPView and one abstract construction 
     19 * class, called DPFactory. An application-level class, called DPApplication
     20 * is responsible for construction. 
     21 * 
     22 * We have a series of application-level operating classes derived from this
     23 * framework - MyDocument, MyWorkspace and MyView. For design reasons we 
     24 * assume we wish to instantiate these from inside DPApplication. As there
     25 * are multiple objects needed and they could be arranged in different 
     26 * lattices, we use a factory, MyFactory (in our example, there are all 
     27 * simple siblings), which is called inside DPApplication. 
     28 * 
     29 */

     30 
     31namespace AbstractFactory_DesignPattern
     32{
     33    using System;
     34
     35    // These classes could be part of a framework,
     36    // which we will call DP
     37    // ===========================================
     38    
     39    abstract class DPDocument 
     40    {
     41        abstract public void Dump();        
     42    }

     43
     44    abstract class DPWorkspace
     45    {
     46        abstract public void Dump();
     47    }

     48    
     49    abstract class DPView 
     50    {
     51        abstract public void Dump();
     52    }
        
     53    
     54    abstract class DPFactory 
     55    {
     56        abstract public DPDocument CreateDocument();
     57        abstract public DPView CreateView();
     58        abstract public DPWorkspace CreateWorkspace();
     59    }

     60
     61    abstract class DPApplication 
     62    {
     63        protected DPDocument doc;
     64        protected DPWorkspace workspace;
     65        protected DPView view;
     66        
     67        public void ConstructObjects(DPFactory factory)
     68        {
     69            // Create objects as needed
     70            doc = factory.CreateDocument();
     71            workspace = factory.CreateWorkspace();
     72            view = factory.CreateView();
     73        }
            
     74        
     75        abstract public void Dump();
     76
     77        public void DumpState()
     78        {
     79            if (doc != null) doc.Dump();
     80            if (workspace != null) workspace.Dump();
     81            if (view != null) view.Dump();
     82        }

     83    }

     84
     85    // These classes could be part of an application 
     86    class MyApplication : DPApplication 
     87    {
     88        MyFactory myFactory = new MyFactory();
     89
     90        override public void Dump()
     91        {
     92            Console.WriteLine("MyApplication exists");
     93        }

     94
     95        public void CreateFamily()
     96        {
     97            MyFactory myFactory = new MyFactory();
     98            ConstructObjects(myFactory);            
     99        }

    100    }
        
    101
    102    class MyDocument : DPDocument 
    103    {
    104        public MyDocument()
    105        {
    106                Console.WriteLine("in MyDocument constructor");            
    107        }

    108        
    109        override public void Dump()
    110        {
    111            Console.WriteLine("MyDocument exists");
    112        }

    113    }

    114
    115    class MyWorkspace : DPWorkspace 
    116    {
    117        override public void Dump()
    118        {
    119            Console.WriteLine("MyWorkspace exists");
    120        }

    121    }

    122
    123    class MyView : DPView 
    124    {
    125        override public void Dump()
    126        {
    127            Console.WriteLine("MyView exists");
    128        }

    129    }

    130
    131    class MyFactory : DPFactory 
    132    {
    133        override public DPDocument CreateDocument()
    134        {
    135            return new MyDocument();
    136        }

    137        override public DPWorkspace CreateWorkspace()
    138        {
    139            return new MyWorkspace();
    140        }
            
    141        override public DPView CreateView()
    142        {
    143            return new MyView();
    144        }

    145    }

    146
    147    /// <summary>
    148    ///    Summary description for Client.
    149    /// </summary>

    150    public class Client
    151    {
    152        public static int Main(string[] args)
    153        {
    154            MyApplication myApplication = new MyApplication();
    155
    156            myApplication.CreateFamily();
    157
    158            myApplication.DumpState();
    159            
    160            return 0;
    161        }

    162    }

    163}

    164
    165
  • 相关阅读:
    JavaScript中的ActiveXObject控制Excel的方法
    事务的作用
    SQL2005关于quotename的用法
    简易办公系统的设计与实现 文献收集
    《UML建模在办公自动化(OA)系统设计中的应用 》论文笔记(四)
    《基于 UML 与 J2EE 的高校 OA 系统的分析与设计》论文笔记(三)
    《浅谈企业办公管理OA系统的设计与实现 》论文笔记(二)
    《基于UML连锁超市OA系统公文流转的设计》论文笔记(五)
    《暗时间》读书笔记
    《无纸化办公管理系统的设计与实现》论文笔记(一)
  • 原文地址:https://www.cnblogs.com/DarkAngel/p/179732.html
Copyright © 2011-2022 走看看