最后一个结构型,享元。没有太多的项目经验,对这种模式只有一种概念上的理解,就是为了节约内存等资源,把可重用的东西只申请一次,然后处处调用,同时用Hash进行管理。
直接上图:
代码:
/********* 大话设计模式上的代码 ************/ abstract class Flyweight{ public abstract void Operation(int extrinsicstate); } class ConcreteFlyweight : Flyweight{ public override void Operation(int extrinsicstate){ Console.WriteLine("具体Flyweight : " + extrinsicstate); } } class UnshareConcreteFlyweight : Flyweight{ public override void Operation(int extrinsicstate){ Console.WriteLine("不共享的具体Flyweight : " + extrinsicstate); } } class FlyweightFactory{ private Hashtable flyweights = new Hashtable(); public FlyweightFactory(){ flyweights.add("X",new ConcreteFlyweight()); flyweights.add("Y",new ConcreteFlyweight()); } public Flyweight GetFlyweight(string key){ return ((Flyweight)flyweights[key]); } }
时间: 2024-10-12 20:34:23