예제 #1
0
        /// <summary>
        /// Entry point into console application.
        /// </summary>
        public void Run()
        {
            // Arbitrary extrinsic state
            int extrinsicstate = 22;

            var factory = new FlyweightFactory();

            // Work with different flyweight instances
            var fx = factory.GetFlyweight("X");

            fx.Operation(--extrinsicstate);

            var fy = factory.GetFlyweight("Y");

            fy.Operation(--extrinsicstate);

            var fz = factory.GetFlyweight("Z");

            fz.Operation(--extrinsicstate);

            var fu = new
                     UnsharedConcreteFlyweight();

            fu.Operation(--extrinsicstate);
        }
예제 #2
0
        //Пристосуванець - Flyweight
        public Run Flyweight()
        {
            Console.WriteLine("\nFlyweight:");

            // Arbitrary extrinsic state
            int extrinsicstate = 22;

            FlyweightFactory factory = new FlyweightFactory();

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

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

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

            UnsharedConcreteFlyweight fu = new
                                           UnsharedConcreteFlyweight();

            fu.Operation(--extrinsicstate);

            return(this);
        }
    /// <summary>
    /// Entry point into console application.
    /// </summary>
    static void Main()
    {
        // Arbitrary extrinsic state
        int extrinsicstate = 22;

        FlyweightFactory factory = new FlyweightFactory();

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

        fx.Operation(--extrinsicstate);

        Flyweight fy = factory.GetFlyweight("Y");

        fy.Operation(--extrinsicstate);

        Flyweight fz = factory.GetFlyweight("Z");

        fz.Operation(--extrinsicstate);

        UnsharedConcreteFlyweight fu = new
                                       UnsharedConcreteFlyweight();

        fu.Operation(--extrinsicstate);

        // Wait for user
        Console.ReadKey();
    }
예제 #4
0
// "FlyweightFactory"

        // "ConcreteFlyweight"

        public void TestFlyWeigthPattern()
        {
            // Arbitrary extrinsic state
            int extrinsicstate = 22;

            var f = new FlyweightFactory();

            // Work with different flyweight instances
            Flyweight fx = f.GetFlyweight("X");

            fx.Operation(--extrinsicstate);

            Flyweight fy = f.GetFlyweight("Y");

            fy.Operation(--extrinsicstate);

            Flyweight fz = f.GetFlyweight("Z");

            fz.Operation(--extrinsicstate);

            var uf = new
                     UnsharedConcreteFlyweight();

            uf.Operation(--extrinsicstate);

            // Wait for user
            Console.Read();
        }
        static void Main(string[] args)
        {
            FlyweightFactory flyweightFactory = new FlyweightFactory();
            IFlyweight       cafe             = flyweightFactory.GetFlyweight("Expresso");

            Console.WriteLine("*************Expresso****************");
            Console.WriteLine("Tipo de Cafe = " + cafe.TipodeCafe());
            Console.WriteLine("Tipo de Liquido = " + cafe.TipodeLiquido());
            Console.WriteLine("Extras = " + cafe.Extras());
            Console.WriteLine("Precio = " + cafe.Precio());
            cafe = flyweightFactory.GetFlyweight("Caramelo Macchiato");
            Console.WriteLine("**********Caramelo Macchiato*********");
            Console.WriteLine("Tipo de Cafe = " + cafe.TipodeCafe());
            Console.WriteLine("Tipo de Liquido = " + cafe.TipodeLiquido());
            Console.WriteLine("Extras = " + cafe.Extras());
            Console.WriteLine("Precio = " + cafe.Precio());
            cafe = flyweightFactory.GetFlyweight("Capuchino con Canela");
            Console.WriteLine("*********Capuchino con Canela********");
            Console.WriteLine("Tipo de Cafe = " + cafe.TipodeCafe());
            Console.WriteLine("Tipo de Liquido = " + cafe.TipodeLiquido());
            Console.WriteLine("Extras = " + cafe.Extras());
            Console.WriteLine("Precio = " + cafe.Precio());
            cafe = flyweightFactory.GetFlyweight("Tea Verde");
            Console.WriteLine("**************Tea Verde**************");
            Console.WriteLine("Tipo de Cafe = " + cafe.TipodeCafe());
            Console.WriteLine("Tipo de Liquido = " + cafe.TipodeLiquido());
            Console.WriteLine("Extras = " + cafe.Extras());
            Console.WriteLine("Precio = " + cafe.Precio());
            Console.ReadKey();
        }
예제 #6
0
    void TestFlyWeight()
    {
        FlyweightFactory factory = new FlyweightFactory();

        FlyWeight flyWeightX = factory.GetFlyweight("酷炫效果");

        flyWeightX.Operation();

        FlyWeight flyWeightY = factory.GetFlyweight("旋转效果");

        flyWeightY.Operation();

        FlyWeight flyWeightZ = factory.GetFlyweight("光影效果");

        flyWeightZ.Operation();

        FlyWeight flyWeightUnshared = new UnsharedConcreteFlyWeight("火焰效果");

        flyWeightUnshared.Operation();

        FlyWeight flyWeightM = factory.GetFlyweight("光影效果");

        flyWeightM.Operation();

        FlyWeight flyWeightN = factory.GetFlyweight("光影效果");

        flyWeightN.Operation();

        Debug.Log("Total number of fly weight : " + factory.GetFlyWeightCount());
    }
예제 #7
0
        public static void FlyweightStructural()
        {
            // Arbitrary extrinsic state
            int extrinsicstate = 22;

            FlyweightFactory factory = new FlyweightFactory();

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

            fx.Operation(--extrinsicstate);

            Flyweight fy = factory.GetFlyweight("Y");

            fy.Operation(--extrinsicstate);

            Flyweight fz = factory.GetFlyweight("Z");

            fz.Operation(--extrinsicstate);

            UnsharedConcreteFlyweight fu = new
                                           UnsharedConcreteFlyweight();

            fu.Operation(--extrinsicstate);
        }
예제 #8
0
        public void Show()
        {
            var factory = new FlyweightFactory();

            var conc1 = factory.GetFlyweight("conc1");
            var conc2 = factory.GetFlyweight("conc1");

            conc1.Operation("1");
            conc2.Operation("2");
        }
예제 #9
0
        public void FlyweightPatternCodeTest()
        {
            FlyweightFactory factory = new FlyweightFactory();

            var flyweightA1 = factory.GetFlyweight("A");

            flyweightA1.Operation();

            var flyweightA2 = factory.GetFlyweight("A");
        }
예제 #10
0
        public void FlyweightTest()
        {
            var factory = new FlyweightFactory();
            var m       = factory.GetFlyweight("A");

            m.Operation(1);

            var d = factory.GetFlyweight("A");

            Console.WriteLine(d.GetState());
        }
예제 #11
0
        static void Main()
        {
            var factory = new FlyweightFactory();

            var flyweight1 = factory.GetFlyweight("A");
            var flyweight2 = factory.GetFlyweight("A");
            var flyweight3 = factory.GetFlyweight("B");

            flyweight1.Display();
            flyweight2.Display();
            flyweight3.Display();
        }
예제 #12
0
        static void Main()
        {
            var factory = new FlyweightFactory();

            var flyweight1 = factory.GetFlyweight("A");
            var flyweight2 = factory.GetFlyweight("A");
            var flyweight3 = factory.GetFlyweight("B");

            flyweight1.Display();
            flyweight2.Display();
            flyweight3.Display();
        }
예제 #13
0
        static void Main(string[] args)
        {
            FlyweightFactory factory = new FlyweightFactory();

            FlyweightContext charAFirstContext = new FlyweightContext(5, 5, "red");

            factory.GetFlyweight("charA").Draw(charAFirstContext);

            FlyweightContext charASndContext = new FlyweightContext(1, 3, "green");

            factory.GetFlyweight("charA").Draw(charASndContext);
        }
예제 #14
0
    // Use this for initialization
    void Start()
    {
        FlyweightFactory flyFactory = new FlyweightFactory();
        Flyweight        flyweight  = flyFactory.GetFlyweight("A");

        Debug.Log(flyweight.GetData());
        Flyweight flyweight2 = flyFactory.GetFlyweight("A"); // 같은데이터 값은 재사용 한다.

        Debug.Log(flyweight2.GetData());
        Flyweight flyweight3 = flyFactory.GetFlyweight("CC"); // 새로운 것만 생성

        Debug.Log(flyweight3.GetData());
    }
예제 #15
0
        static void Main(string[] args)
        {
            int state = 0;
            FlyweightFactory factory   = new FlyweightFactory();
            Flyweight        flyweight = factory.GetFlyweight("2");

            flyweight.Operation(state);
            flyweight = factory.GetFlyweight("228");
            flyweight.Operation(state);

            flyweight = new UnsharedFlyweight();
            flyweight.Operation(state);
        }
예제 #16
0
        static void FlyweightExample()
        {
            var flyweightFactory = new FlyweightFactory();
            var flyweight1       = flyweightFactory.GetFlyweight("Test", 11);
            var flyweight2       = flyweightFactory.GetFlyweight("Test", 12);

            Console.WriteLine(flyweight1 == flyweight2);

            flyweight1.Operation(5);
            flyweight2.Operation(6);

            var flyweight3 = new UnsharedFlyweight();

            flyweight3.Operation(7);
        }
예제 #17
0
        static void Main(string[] args)
        {
            int exState = 0;
            AbstractFlyweight flyweight = null;
            FlyweightFactory  factory   = new FlyweightFactory();

            flyweight = factory.GetFlyweight("1");
            flyweight.Operation(exState);

            flyweight = factory.GetFlyweight("10");
            flyweight.Operation(exState);

            flyweight = new UnsharedConctreteFlyweight();
            flyweight.Operation(exState);
        }
예제 #18
0
        public UseCase()
        {
            int extrinsicState = 0;

            AbstractFlyweight flyweight;
            FlyweightFactory  factory = new FlyweightFactory();

            flyweight = factory.GetFlyweight("1");
            flyweight.Operation(extrinsicState);

            flyweight = factory.GetFlyweight("10");
            flyweight.Operation(extrinsicState);

            flyweight = new ConcreteFlyweightUnshared();
            flyweight.Operation(extrinsicState);
        }
예제 #19
0
        static void Main()
        {
            var extrinsicState = 0;

            Flyweight flyweight = null;
            var       factory   = new FlyweightFactory();

            flyweight = factory.GetFlyweight("1");
            flyweight.Operation(extrinsicState);

            flyweight = factory.GetFlyweight("10");
            flyweight.Operation(extrinsicState);

            flyweight = new UnsharedConcreteFlyweight();
            flyweight.Operation(extrinsicState);
        }
예제 #20
0
        public void mem(int n)
        {
            //Flyweight atminties matavimas
            FlyweightFactory f = new FlyweightFactory();

            State[] s  = new State[n];
            State[] s1 = new State[n];
            GC.Collect();
            GC.Collect();
            GC.Collect();
            long kbBefore = GC.GetTotalMemory(true);

            for (int i = 0; i < n; i++)
            {
                s[i] = f.GetFlyweight('W');
            }
            long kbAfter1 = GC.GetTotalMemory(false);
            long kbAfter2 = GC.GetTotalMemory(true);

            System.Diagnostics.Debug.WriteLine("Flyweight: memory usage with flyweight: n = {0}, {1}", n, kbAfter2 - kbBefore);
            GC.Collect();
            GC.Collect();
            GC.Collect();
            kbBefore = GC.GetTotalMemory(true);
            for (int i = 0; i < n; i++)
            {
                s1[i] = new Waiting();
            }
            kbAfter1 = GC.GetTotalMemory(false);
            kbAfter2 = GC.GetTotalMemory(true);
            System.Diagnostics.Debug.WriteLine("Flyweight: memory usage without flyweight: n = {0}, {1}", n, kbAfter2 - kbBefore);
        }
예제 #21
0
        static void Main(string[] args)
        {
            #region 字符串 外部
            //string key1 = "科目1上海";
            //string key2 = "科目1上海";

            //FlyweightFactory.GetFlyweight(key1);
            //var sw = Stopwatch.StartNew();
            //for (int i = 0; i < 1000000; i++)
            //{
            //    FlyweightFactory.GetFlyweight(key2);
            //}
            //Console.WriteLine("Time: {0}ms", sw.ElapsedMilliseconds);
            #endregion
            ExtrinsicState state1 = new ExtrinsicState();
            state1.SetSubject("科目1");
            state1.SetLocation("上海");
            FlyweightFactory.GetFlyweight(state1);

            ExtrinsicState state2 = new ExtrinsicState();
            state2.SetSubject("科目1");
            state2.SetLocation("上海");

            var sw = Stopwatch.StartNew();
            for (int i = 0; i < 1000000; i++)
            {
                FlyweightFactory.GetFlyweight(state2);
            }
            Console.WriteLine("Time: {0}ms", sw.ElapsedMilliseconds);

            Console.ReadKey();
        }
예제 #22
0
파일: Program.cs 프로젝트: schangxiang/Code
        static void FlyWeightInvoke()
        {
            int externalstate        = 10;                     // 定义外部状态,如字母的位置等信息
            FlyweightFactory factory = new FlyweightFactory(); // 初始化享元工厂

            Flyweight fa = factory.GetFlyweight("A");

            fa.Operation(--externalstate);

            Flyweight fb = factory.GetFlyweight("B");

            fb.Operation(--externalstate);

            Flyweight fd = factory.GetFlyweight("D");

            fd.Operation(--externalstate);
        }
예제 #23
0
    public static void Main(string[] args)
    {
        FlyweightFactory flyweightFactory = new FlyweightFactory();

        IFlyweight[] flyweights = new IFlyweight[]
        {
            flyweightFactory.GetFlyweight("A"),
            flyweightFactory.GetFlyweight("A"),
            flyweightFactory.GetFlyweight("B"),
            flyweightFactory.GetFlyweight("C"),
            flyweightFactory.GetFlyweight("C")
        };

        foreach (IFlyweight flyweight in flyweights)
        {
            flyweight.Operation();
        }
    }
예제 #24
0
        public void RestoreState()
        {
            //Flyweight naudojimas:
            FlyweightFactory ff = new FlyweightFactory();

            this.State2 = new ContextState(ff.GetFlyweight('W'));
            System.Diagnostics.Debug.WriteLine("Flyweight: Waiting");
            //State naudojimas be flyweight:
            this.State2 = new ContextState(new Waiting());
        }
예제 #25
0
        static void FlyweightTester()
        {
            #region sample 1
            var extrinsicstate = 22;

            var factory = new FlyweightFactory();

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

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

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

            var fu = new
                     UnsharedConcreteFlyweight();

            fu.Operation(--extrinsicstate);
            #endregion

            #region sample 2
            // Build a document with text
            const string document = "AAZZBBZB";
            var          chars    = document.ToCharArray();

            var factoryImplementation = new CharacterFactory();

            // extrinsic state
            var pointSize = 10;

            // For each character use a flyweight object
            foreach (var c in chars)
            {
                pointSize++;
                var character = factoryImplementation.GetCharacter(c);
                character.Display(pointSize);
            }
            #endregion
        }
예제 #26
0
        public void Start()
        {
            FlyweightFactory factory = new FlyweightFactory();

            FlyweightModel car1 = factory.GetFlyweight("Red", "Sedan");

            car1.Draw();

            FlyweightModel car2 = factory.GetFlyweight("Green", "Coupe");

            car2.Draw();

            FlyweightModel car3 = factory.GetFlyweight("Green", "Coupe");

            car3.Draw();

            FlyweightModel car4 = factory.GetFlyweight("Blue", "Sedan");

            car4.Draw();
        }
        string AddCarToPoliceDatabase(FlyweightFactory factory, Car car)
        {
            var flyweight = factory.GetFlyweight(new Car
            {
                Color   = car.Color,
                Model   = car.Model,
                Company = car.Company
            });

            return(flyweight.Operation(car));
        }
예제 #28
0
        static void Main(string[] args)
        {
            int extrinsicstate  = 22;
            FlyweightFactory f  = new FlyweightFactory();
            Flyweight        fx = f.GetFlyweight("X");

            fx.Operation(--extrinsicstate);

            Flyweight fy = f.GetFlyweight("Y");

            fy.Operation(--extrinsicstate);

            Flyweight fz = f.GetFlyweight("Z");

            fz.Operation(--extrinsicstate);
            UnsharedConcreteFlyweight uf = new UnsharedConcreteFlyweight();

            uf.Operation(--extrinsicstate);
            Console.Read();
        }
예제 #29
0
        public static void FlyweightPattern()
        {
            int extrinsicatate = 22;
            FlyweightFactory f = new FlyweightFactory();

            Flyweight fx = f.GetFlyweight("X");

            fx.Operation(--extrinsicatate);
            Flyweight fy = f.GetFlyweight("Y");

            fx.Operation(--extrinsicatate);
            Flyweight fz = f.GetFlyweight("Z");

            fx.Operation(--extrinsicatate);

            Flyweight uf = new UnsharedConcreteFlyweight();

            uf.Operation(--extrinsicatate);

            Console.Read();
        }
예제 #30
0
    static void Main()
    {
        int extrinsicstate  = 22;
        FlyweightFactory f  = new FlyweightFactory();
        Flyweight        fx = f.GetFlyweight("X");

        fx.Operation(--extrinsicstate);
        Flyweight uf = new UnshareConcreteFlyweight();

        uf.Operation(--extrinsicstate);
        Console.ReadKey();
    }
예제 #31
0
  public static void Main( string[] args )
  {
    // Arbitrary extrisic state
    int extrinsicstate = 22;
		
    FlyweightFactory f = new FlyweightFactory();

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

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

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

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

  }
예제 #32
0
        public void MethodName()
        {
            var extrinsicstate = 22;

            var factory = new FlyweightFactory();

            var fX = factory.GetFlyweight("X");

            fX.Operation(--extrinsicstate);

            var fY = factory.GetFlyweight("Y");

            fY.Operation(--extrinsicstate);

            var fZ = factory.GetFlyweight("Z");

            fY.Operation(--extrinsicstate);

            var fU = new UnsharedConcreteFlyweight();

            fU.Operation(--extrinsicstate);
        }
예제 #33
0
    // Entry point into console application.
    static void Main()
    {
        // Arbitrary extrinsic state
        int extrinsic_state = 22;

        factory = new FlyweightFactory();

        // Work with different flyweight instances

        ch = factory.GetFlyweight("X");
        ch.Operation(--extrinsic_state);

        ch = factory.GetFlyweight("Y");
        ch.Operation(--extrinsic_state);

        ch = factory.GetFlyweight("Z");
        ch.Operation(--extrinsic_state);

        ch = new UnsharedConcreteFlyweight();
        ch.Operation(--extrinsic_state);

        // Wait for user
        Console.ReadKey();
    }