C#设计模式之享元模式(Flyweight)
using System; using System.Collections; public class Client { public static void Main(string[] args) { /* 我的第一个 C# 程序*/ int extrinsicstate=22; FlyweigrhFactory f=new FlyweigrhFactory(); //work with different flyweight instance Flyweight fx=f.GetFlyweight("X"); fx.Operation(--extrinsicstate); Flyweight fy=f.GetFlyweight("Y"); fx.Operation(--extrinsicstate); Flyweight fz=f.GetFlyweight("Z"); fx.Operation(--extrinsicstate); } } public class FlyweigrhFactory { private Hashtable flyweights=new Hashtable(); public FlyweigrhFactory() { flyweights.Add("X",new ConcreteFlyweight()); flyweights.Add("Y",new ConcreteFlyweight()); flyweights.Add("Z",new ConcreteFlyweight()); } public Flyweight GetFlyweight(string key) { return ((Flyweight)flyweights[key]); } } public abstract class Flyweight { abstract public void Operation(int extrinsicstate); } public class ConcreteFlyweight:Flyweight { private string intrinsicstate="A"; override public void Operation(int extrinsicstate) { Console.WriteLine("ConcreteFlyweight:intrinsicstate {0},extrinsicstate {1}",intrinsicstate,extrinsicstate); } }
上述代码为示例,暂存