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());
        }
      }
  • 相关阅读:
    Pyhon基础过滤字符串中的字母数字特殊符号
    [编程题] 斐波那契数列
    左旋转字符串(Java)-循环Index方式
    [编程题]字符流中第一个不重复的字符
    6525. 【2020.4.1模拟】Valleys
    6515. 【GDOI2020模拟03.28】数一数(one)
    6516. 【GDOI2020模拟03.28】数二数(two)
    6508. 【GDOI2020模拟03.11】我的朋友们
    6494. 【GDOI2020模拟03.08】勘探
    6491. 【GDOI2020模拟03.04】铺路
  • 原文地址:https://www.cnblogs.com/shihao/p/2496307.html
Copyright © 2011-2022 走看看