zoukankan      html  css  js  c++  java
  • flyweighttheoryns.cs

      using System;
      using System.Collections.Generic;
      using System.Drawing;
      using System.Windows.Forms;

      namespace FlyweightPattern {

        // Flyweight Pattern        Judith Bishop  Sept 07
        // Defined for state which is images
     
        // IFlyweight Interface
        public interface IFlyweight {
          void Load (string filename);
          void Display (PaintEventArgs e, int row, int col);
        }

        // Flyweight
        public struct Flyweight : IFlyweight {
          // Intrinsicstate
          Image pThumbnail;
          public void Load (string filename) {
            pThumbnail = new Bitmap("images/"+filename).
                               GetThumbnailImage(100, 100, null, new IntPtr());
          }

          public void Display(PaintEventArgs e, int row, int col) {
            // Calculating extrinsic state
            e.Graphics.DrawImage(pThumbnail,col*100+10, row*130+40,
                                           pThumbnail.Width,pThumbnail.Height);
          }
        }

        public class FlyweightFactory {
          // Keeps an indexed list of IFlyweight objects in existance
          Dictionary <string,IFlyweight> flyweights =
              new Dictionary <string,IFlyweight> ();

          public FlyweightFactory () {
            flyweights.Clear();
          }

          public IFlyweight this[string index] {   
            get {  
              if (!flyweights.ContainsKey(index))
                  flyweights[index] = new Flyweight();
              return flyweights[index];
            }  
          }
        }
      }
  • 相关阅读:
    《梦断代码》读后感(二)
    《梦断代码》读后感(一)
    最大子数组和(环状数组)
    求最大字数组的和的1000个元素的新要求
    面试题fugui02
    Linux基本操作
    面试题fugui
    水星的水
    python魔术方法
    tornado websocket聊天室
  • 原文地址:https://www.cnblogs.com/shihao/p/2496413.html
Copyright © 2011-2022 走看看