示例#1
0
        public ConcreteFlyweight factory(string name)
        {
            ConcreteFlyweight fly = null;

            _dict.TryGetValue(name, out fly);
            if (fly == null)
            {
                fly = new ConcreteFlyweight(name);
                _dict.Add(name, fly);
            }
            return(fly);
        }
示例#2
0
        public static void invoke()
        {
            FlyweightFactory  factory = new FlyweightFactory();
            ConcreteFlyweight fly     = factory.factory("vv");

            fly.Index = 1;
            fly.operation();

            ConcreteFlyweight fly2 = factory.factory("xx");

            fly2.Index = 2;
            fly2.operation();


            ConcreteFlyweight fly3 = factory.factory("vv");

            fly3.Index = 3;
            fly3.operation();
            fly.operation();
            Console.WriteLine(object.ReferenceEquals(fly, fly3));
        }