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());
博客展示了享元模式的代码实现。定义了 flyweight 接口和 Concerateflyweight 类,还创建了 flyweightFactory 类用于管理享元对象。通过工厂获取不同键的享元对象,并输出其哈希码,体现了享元模式的应用。
70

被折叠的 条评论
为什么被折叠?



