static void Main(string[] args) { //定义外部状态, int externalstate = 10; //初始化享元工厂 FlyweightFactory factory = new FlyweightFactory(); //判断是否已经创建了字母A,如果已经创建就直接使用床架你的对象A Flyweight fa = factory.GetFlyweight("A"); if (fa != null) { //把外部状态作为享元对象的方法调用参数 fa.Operation(externalstate); } //判断是否已经创建了字母D Flyweight fd = factory.GetFlyweight("D"); if (fd != null) { fd.Operation(externalstate); } else { Console.WriteLine("驻留池中不存在对象D"); //这时候就需要创建一个对象并放入驻留池中 ConcreteFlyweight d = new ConcreteFlyweight("D"); factory.flyweights.Add("D", d); } Console.ReadKey(); }
static void Main(string[] args) { ConcreteFlyweight concreteFlyweight = new ConcreteFlyweight(); UnsharedObject1 unsharedObject1 = new UnsharedObject1(concreteFlyweight); UnsharedObject1 unsharedObject2 = new UnsharedObject1(concreteFlyweight); unsharedObject1.Method("Работа первого объекта через flyweight"); unsharedObject2.Method("Работа второго объекта через flyweight"); }
public FlyweightBase GetFlyweight(string key) { if (_flyweights.Contains(key)) { return(_flyweights[key] as FlyweightBase); } var newFlyweight = new ConcreteFlyweight(); _flyweights.Add(key, newFlyweight); return(newFlyweight); }
public FlyweightBase GetFlyweight(string key) { if (_flyweights.Contains(key)) { return _flyweights[key] as FlyweightBase; } var newFlyweight = new ConcreteFlyweight(); _flyweights.Add(key, newFlyweight); return newFlyweight; }
static void Main(string[] args) { // 定义外部状态,例如字母的位置等信息 int extrinsicstate = 10; // 初始化享元工厂 FlyweightFactory factory = new FlyweightFactory(); // 判断是否创建了字母A,如果已经创建就直接使用创建的对象A Flyweight fa = factory.GetFlyweight("A"); if (fa != null) { // 把外部对象作为享元对象的方法调用参数 fa.Operation(--extrinsicstate); } // 判断是否创建了字母B,如果已经创建就直接使用创建的对象B Flyweight fb = factory.GetFlyweight("B"); if (fb != null) { fb.Operation(--extrinsicstate); } // 判断是否创建了字母C,如果已经创建就直接使用创建的对象C Flyweight fc = factory.GetFlyweight("C"); if (fc != null) { fc.Operation(--extrinsicstate); } // 判断是否创建了字母D,如果已经创建就直接使用创建的对象D Flyweight fd = factory.GetFlyweight("D"); if (fd != null) { fd.Operation(--extrinsicstate); } else { Console.WriteLine("驻留池中不存在字符串D"); // 这时候需要创建一个对象并放入驻留池中 ConcreteFlyweight d = new ConcreteFlyweight("D"); factory.flyweights.Add("D", d); } Console.ReadLine(); }
public Flyweight GetFlyweight(string key) { Flyweight flyweight; if (flyweights.ContainsKey(key) == false) { Console.WriteLine("驻留池中不存在字符串---" + key); flyweight = new ConcreteFlyweight(key); } else { flyweight = flyweights[key] as Flyweight; } return(flyweight); }