zoukankan      html  css  js  c++  java
  • 工厂模式


    名称 Factory Method
    结构
    意图 定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method 使一个类的实例化延迟到其子类。
    适用性
    • 当一个类不知道它所必须创建的对象的类的时候。
    • 当一个类希望由它的子类来指定它所创建的对象的时候。
    • 当类将创建对象的职责委托给多个帮助子类中的某一个,并且你希望将哪一个帮助子类是代理者这一信息局部化的时候。

     example

     1// Factory Method
     2
     3// Intent: "Define an interface for creating an object, but let subclasses 
     4// decide which class to instantiate. Factory Method lets a class defer 
     5// instantiation to subclasses". 
     6
     7// For further information, read "Design Patterns", p107, Gamma et al.,
     8// Addison-Wesley, ISBN:0-201-63361-2
     9
    10/* Notes:
    11* This design pattern is often used in a framework architecture, which
    12* contains a number of related classes inside the framework, and which
    13* expects application developers to derive custom classes from them.
    14* If the instantiation of some custom application classes needs to be
    15* carried out inside framework code, then the Factory Method design
    16* pattern is an ideal solution.
    17
    18* The sample below is based on a document view architecture. 
    19* It shows the framework providing classes DPApplication and DPDocument
    20* and the application providing MyApplication and MyDocument, derived
    21* from the corresponding framework classes. Note how the construction
    22* of MyDocument is initiated from inside the framework's DPApplication,
    23* yet the framework was not written specifically for it.  
    24
    25* It could be extended for views and workspaces along the same lines. 
    26*/

    27
    28namespace FactoryMethod_DesignPattern
    29{
    30    using System;
    31
    32    // These two classes could be part of a framework,
    33    // which we will call DP
    34    // ===============================================
    35
    36    class DPDocument 
    37    {
    38
    39
    40    }

    41
    42    abstract class DPApplication 
    43    {
    44        protected DPDocument doc;
    45
    46        abstract public void CreateDocument();
    47
    48        public void ConstructObjects()
    49        {
    50
    51            // Create objects as needed
    52            // . . .
    53
    54            // including document
    55            CreateDocument();
    56        }
     
    57        abstract public void Dump();
    58    }

    59
    60    // These two classes could be part of an application 
    61    // =================================================
    62
    63    class MyApplication : DPApplication 
    64    {
    65        override public void CreateDocument()
    66        {
    67            doc = new MyDocument(); 
    68        }
     
    69
    70        override public void Dump()
    71        {
    72            Console.WriteLine("MyApplication exists");
    73        }

    74    }
     
    75
    76    class MyDocument : DPDocument 
    77    {
    78
    79    }

    80
    81    /// <summary>
    82    ///    Summary description for Client.
    83    /// </summary>

    84    public class Client
    85    {
    86        public static int Main(string[] args)
    87        {
    88            MyApplication myApplication = new MyApplication();
    89
    90            myApplication.ConstructObjects();
    91
    92            myApplication.Dump();
    93
    94            return 0;
    95        }

    96    }

    97
  • 相关阅读:
    二分搜索树的深度优先遍历和广度优先遍历
    数据结构与算法之非比较排序【Java】
    数据结构与算法之比较排序【Java】
    像素 转换 px dp
    Toast
    MySQL丶auto_increment
    MariaDB · 性能优化 · Extended Keys
    加唯一索引怎么会导致丢数据
    语句执行错误一· Count(Distinct) ERROR
    innodb参数 &#183; innodb_flush_log_at_trx_commit
  • 原文地址:https://www.cnblogs.com/DarkAngel/p/179727.html
Copyright © 2011-2022 走看看