CSDN博客

img 2002pine

Design Pattern 4-flyweight

发表于2004/9/25 17:13:00  641人阅读

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());

阅读全文
0 0

相关文章推荐

img
取 消
img