zoukankan      html  css  js  c++  java
  • 设计模式——享元模式

    名称 Flyweight
    结构  
    意图 运用共享技术有效地支持大量细粒度的对象。
    适用性
    • 一个应用程序使用了大量的对象。
    • 完全由于使用大量的对象,造成很大的存储开销。
    • 对象的大多数状态都可变为外部状态。
    • 如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象。
    • 应用程序不依赖于对象标识。由于F l y w e i g h t 对象可以被共享,对于概念上明显有别的对象,标识测试将返回真值。
    Code Example
     1// Flyweight
     2
     3// Intent: "Use sharing to support large numbers of fine-grained objects effectively". 
     4
     5// For further information, read "Design Patterns", p195, Gamma et al.,
     6// Addison-Wesley, ISBN:0-201-63361-2
     7
     8/* Notes:
     9 * Useful patetn when you have a small(ish) number of objects that might be 
    10 * needed a very large number of times - with slightly differnt informaiton,
    11 * that can be externalised outside those objects. 
    12 */

    13 
    14namespace Flyweight_DesignPattern
    15{
    16    using System;
    17    using System.Collections;
    18
    19    class FlyweightFactory 
    20    {
    21        private ArrayList pool = new ArrayList();
    22
    23        // the flyweightfactory can crete all entries in the pool at startup 
    24        // (if the pool is small, and it is likely all will be used), or as 
    25        // needed, if the pool si large and it is likely some will never be used
    26        public FlyweightFactory()
    27        {
    28            pool.Add(new ConcreteEvenFlyweight());        
    29            pool.Add(new ConcreteUnevenFlyweight());            
    30        }

    31
    32        public Flyweight GetFlyweight(int key)
    33        {
    34            // here we would determine if the flyweight identified by key 
    35            // exists, and if so return it. If not, we would create it. 
    36            // As in this demo we have implementation all the possible 
    37            // flyweights we wish to use, we retrun the suitable one. 
    38            int i = key % 2;
    39            return((Flyweight)pool[i]); 
    40        }

    41    }

    42
    43    abstract class Flyweight 
    44    {
    45        abstract public void DoOperation(int extrinsicState);        
    46    }

    47
    48    class UnsharedConcreteFlyweight : Flyweight
    49    {
    50        override public void DoOperation(int extrinsicState)
    51        {
    52            
    53        }

    54    }

    55
    56    class ConcreteEvenFlyweight : Flyweight
    57    {
    58        override public void DoOperation(int extrinsicState)
    59        {
    60            Console.WriteLine("In ConcreteEvenFlyweight.DoOperation: {0}", extrinsicState);                        
    61        }

    62    }

    63
    64    class ConcreteUnevenFlyweight : Flyweight
    65    {
    66        override public void DoOperation(int extrinsicState)
    67        {
    68            Console.WriteLine("In ConcreteUnevenFlyweight.DoOperation: {0}", extrinsicState);            
    69        }
        
    70    }

    71
    72    /// <summary>
    73    ///    Summary description for Client.
    74    /// </summary>

    75    public class Client
    76    {
    77        public static int Main(string[] args)
    78        {
    79            int[] data = {1,2,3,4,5,6,7,8};
    80            
    81            FlyweightFactory f = new FlyweightFactory();
    82            
    83            int extrinsicState = 3;
    84            foreach (int i in data)
    85            {
    86                Flyweight flyweight = f.GetFlyweight(i);
    87                  flyweight.DoOperation(extrinsicState);
    88            }

    89            
    90            return 0;
    91        }

    92    }

    93}

    94
    95
  • 相关阅读:
    callable函数,检查对象是否可调用
    eval函数的一些用法
    divmod函数使用
    sorted(x, reverse=True)
    列表、元组、字典空格的几种移除方法
    约瑟夫环问题(通过观察得出递推式从而建立递归求解)
    快速幂算法(二分思想减少连乘次数)
    素数筛(埃氏筛法与欧拉筛)
    KMP算法的详细解释
    对于线性代数的形象化理解(1)
  • 原文地址:https://www.cnblogs.com/DarkAngel/p/188309.html
Copyright © 2011-2022 走看看