Design Pattern 4-flyweight

using System;

namespace Pattern
{
 /// <summary>
 /// Summary description for Class1.
 /// </summary>
 public interface flyweight
 {
  void act();
 }
 public class Concerateflyweight:flyweight
 {
  public Concerateflyweight()
  {
  }
  public void act()
  {
   System.Console.WriteLine("Concerate flyweight");
  }
 }
 public class flyweightFactory
 {

  System.Collections.Hashtable ht=new System.Collections.Hashtable();
  public  flyweightFactory()
  {
  }
  public flyweight getinstance(string key)
  {
   if (ht[key]==null)
   {
    ht.Add(key,new Concerateflyweight());
   }
   return (flyweight)ht[key];

  }
 }
}
    //享元模式 
   flyweightFactory factory=new flyweightFactory();
  Console.WriteLine(factory.getinstance("aaa").GetHashCode().ToString());
 Console.WriteLine(factory.getinstance("bbb").GetHashCode().ToString());
 Console.WriteLine(factory.getinstance("aaa").GetHashCode().ToString());

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章