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

      using System;
      using System.Collections.Generic;
      using System.Drawing;
      using System.Windows.Forms;
      using FlyweightPattern;
     
      // The Client
      class Client {
        // Shared state - the images
        static FlyweightFactory album =  new FlyweightFactory();
     
        // Unshared state - the groups
        static Dictionary <string,List<string>> allGroups =
                 new Dictionary <string,List<string>> ();  
     
        public void LoadGroups () {
          var myGroups = new [] {
              new  {Name = "Garden",  
                        Members =   new [] {"pot.jpg", "spring.jpg", "barbeque.jpg", "flowers.jpg"}},
              new  {Name = "Italy",  
                        Members =   new [] {"cappucino.jpg","pasta.jpg", "restaurant.jpg", "church.jpg"}},
              new  {Name = "Food",  
                        Members =  new [] {"pasta.jpg", "veggies.jpg", "barbeque.jpg","cappucino.jpg","lemonade.jpg" }},
              new  {Name = "Friends",  
                        Members = new [] {"restaurant.jpg", "dinner.jpg"}}
          };

          // Load the Flyweights, saving on shared intrinsic state
          foreach (var g in myGroups) {
            allGroups.Add(g.Name,new List <string>());
            foreach (string filename in g.Members) {
              allGroups[g.Name].Add(filename);
              album[filename].Load(filename);
            }
          }
        }
     
        public void DisplayGroups (Object source, PaintEventArgs e) {
          // Display the Flyweights, passing the  unshared state
          var row = 0;
          foreach(string g in allGroups.Keys) {
            var col = 0;
            e.Graphics.DrawString(g,
               new Font("Arial", 16),
               new SolidBrush(Color.Black),
               new PointF(0, row*130+10));
            foreach (string filename in allGroups[g]) {
              album[filename].Display(e, row, col);
              col++;
            }
            row++;
          }
        }
      }
        
      class Window : Form {
        Window() {
          this.Height = 600;
          this.Width = 600;
          this.Text = "Picture Groups";
          Client client = new Client();
          client.LoadGroups();
          this.Paint +=  new PaintEventHandler (client.DisplayGroups);
        }
     
        static void Main () {
          Application.Run(new Window());
        }
      }
  • 相关阅读:
    从程序员到项目经理(1)
    从程序员到项目经理(26):项目管理不能浑水摸鱼
    职场“常青树”越老越值钱
    阿里巴巴离职DBA 35岁总结的职业生涯
    CEPH RGW多 ZONE的配置
    CEPH 对象存储的系统池介绍
    Windows 下配置 Vagrant 环境
    vagrant 创建虚拟机时遇到问题
    浅谈Ceph纠删码
    磁盘缓存
  • 原文地址:https://www.cnblogs.com/shihao/p/2496307.html
Copyright © 2011-2022 走看看