Пример #1
0
        static void Main(string[] args)
        {
            #region 结构实现
            // Arbitrary extrinsic state
            int extrinsicstate       = 22;
            FlyweightFactory factory = new FlyweightFactory();

            // Work with different flyweight instances
            Structural.Flyweight fx = factory.GetFlyweight("X");
            fx.Operation(--extrinsicstate);

            Structural.Flyweight fy = factory.GetFlyweight("Y");
            fy.Operation(--extrinsicstate);

            Structural.Flyweight fz = factory.GetFlyweight("Z");
            fz.Operation(--extrinsicstate);

            UnsharedConcreteFlyweight fu = new UnsharedConcreteFlyweight();
            fu.Operation(--extrinsicstate);
            #endregion

            Console.WriteLine("******************************");

            #region 实践应用
            #endregion

            Console.ReadKey();
        }
Пример #2
0
        static void Main()
        {
            /*
             *  Structural Flyweight usage
             */

            int extrinsicState = 21;

            StructuralFlyweightFactory factory = new StructuralFlyweightFactory();

            // work with different flyweight instances
            Structural.Flyweight fx = factory.GetFlyweight("X");
            fx.Operation(--extrinsicState);

            Structural.Flyweight fy = factory.GetFlyweight("Y");
            fy.Operation(--extrinsicState);

            Structural.Flyweight fz = factory.GetFlyweight("Z");
            fz.Operation(--extrinsicState);

            UnsharedConcreteFlyweight fUnshared = new UnsharedConcreteFlyweight();

            fUnshared.Operation(--extrinsicState);

            /*
             *  Real-World flyweight
             */

            // Build a document with text
            string document = "AACCBBCB";

            char[] chars = document.ToCharArray();

            RealWorldFlyweightFactory characterFactory = new RealWorldFlyweightFactory();

            // extrinsic state
            int pointSize = 10;

            // for each character, use a flyweight object
            foreach (char c in chars)
            {
                pointSize++;
                Character character = characterFactory.GetCharacter(c);
                character.Display(pointSize);
            }
        }