设计模式之:享元模式FlyweightPattern的实现
享元模式的理解:
享元模式的定义:运用共享技术支持大量细粒度对象的复用;
Flyweight Pattern Definition:Use sharing to support large numbers of fine-grained efficiently.
享元模式关键词:大量、细粒度、复用、享元池、享元工厂;
- 当系统中存在大量的细粒度的相同或相似对象时,可以使用享元模式;
- 享元模式通过共享技术,实现相同或相似对象的重复利用;
- 享元的字面理解:享也就是分享共享的意思,元就是共享的元素、对象;
- Flyweight的字面理解:Flyweight本意是拳击运动的一个术语,就是蝇量级的意思,flyweight 蝇量级 112磅;
- 享元模式也叫轻量级模式,享元是对Flyweight的意译,直译的话应该叫蝇量级模式;
- 英文定义中采用Flyweight,是想表达对象的粒度,也就是fine-grained细粒度的意思;
- grain本意表示谷物,grained表示像谷物那种颗粒状态,即粒度,而fine-grained则表示细粒度,例如fine grained soil 细土、coarse grained soil粗粒土;
- 享元模式和Unity中的预制体作用类似,享元模式可以通过共享元素生成多个对象,Unity同样可以通过Prefabs生成成千上万的怪物;
- 还有诸如对象池、线程池,实际上也是享元模式的使用案例;
类图with StarUML
棋子抽象类和2个实现类
internal abstract class Chessman
{
public abstract string GetColor();
public void Display() { Console.WriteLine($"棋子颜色{this.GetColor()}"); }
}
internal class BlackChessman : Chessman
{
public override string GetColor() { return "黑色"; }
}
internal class WhiteChessman : Chessman
{
public override string GetColor() { return "白色"; }
}
享元工厂类
internal class ChessmanFactory
{
//饿汉式单例模式
private static ChessmanFactory instance = new ChessmanFactory();
//该字典相当于享元池(对象池)Flyweight Pool
private Dictionary<string, Chessman> dictionary;
//构造注入依赖项Chessman/BlackChessman/WhiteChessman
private ChessmanFactory()
{
dictionary = new Dictionary<string, Chessman>();
Chessman black = new BlackChessman();
Chessman white = new WhiteChessman();
dictionary.Add("b", black);
dictionary.Add("w", white);
}
//返回唯一实例
public static ChessmanFactory GetInstance() { return instance; }
//根据键是b还是w,返回字典中的对应棋子
public Chessman GetChessman(string color) { return dictionary[color]; }
}
客户端
internal class Program
{
static void Main(string[] args)
{
Chessman black1, black2, white1, white2;
ChessmanFactory factory = ChessmanFactory.GetInstance();
//生成两颗黑子,并比较
black1 = factory.GetChessman("b");
black2 = factory.GetChessman("b");
Console.WriteLine($"两颗黑子是否相同?{black1 == black2}");
//生成两颗白字,并比较
white1 = factory.GetChessman("w");
white2 = factory.GetChessman("w");
Console.WriteLine($"两颗白子是否相同?{black1 == black2}");
//显示棋子
black1.Display();
black2.Display();
white1.Display();
white2.Display();
Console.Read();
}
}
运行结果