Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // 定义外部状态,例如字母的位置等信息
            int externalstate = 10;

            string[]         strings = new[] { "A", "B", "C", "D" };
            Random           random  = new Random();
            FlyweightFactory factory = new FlyweightFactory();

            for (int i = 0; i < 10; i++)
            {
                var       s  = strings[random.Next(4)];
                Flyweight fd = factory.GetFlyweight(s);
                if (fd != null)
                {
                    fd.Operate(--externalstate);
                }
                else
                {
                    Console.WriteLine("{0}加入驻留池", s);
                    factory.Flyweights.Add("D", new ConcreteFlyweight(s));
                }
            }

            Console.ReadKey();
        }