Esempio n. 1
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();
        }
Esempio n. 2
0
    public static void Main()
    {
        string           extrinsicstate = "THIS IS A BIG EXTRINSIC STATE";
        FlyweightFactory factory        = new FlyweightFactory();

        UnsharedConcreteFlyweight flyweights1 = new UnsharedConcreteFlyweight(new Flyweight[]
        {
            factory.GetFlyweight('a'),
            factory.GetFlyweight('a'),
            factory.GetFlyweight('b'),
            factory.GetFlyweight('c'),
            factory.GetFlyweight('a'),
            factory.GetFlyweight('c')
        });

        UnsharedConcreteFlyweight flyweights2 = new UnsharedConcreteFlyweight(new Flyweight[]
        {
            factory.GetFlyweight('c'),
            factory.GetFlyweight('b'),
            factory.GetFlyweight('b'),
            factory.GetFlyweight('a'),
            factory.GetFlyweight('c'),
            factory.GetFlyweight('b')
        });

        flyweights1.Operation(extrinsicstate);
        flyweights2.Operation(extrinsicstate);

        Console.ReadKey();
    }
        /// <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);
        }
    /// <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();
    }
Esempio n. 5
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);
        }
        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);
        }
Esempio n. 7
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);
        }
Esempio n. 8
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
        }
Esempio n. 9
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();
        }
Esempio n. 10
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();
        }
Esempio n. 11
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 );

  }
Esempio n. 12
0
            void Main()
            {
                int extrinsicstate = 22;

                FlyweightFactory f = new FlyweightFactory();

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

                fx.Operation(--extrinsicstate);

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

                fy.Operation(--extrinsicstate);

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

                fd.Operation(--extrinsicstate);

                UnsharedConcreteFlyweight uf = new UnsharedConcreteFlyweight();

                uf.Operation(--extrinsicstate);
            }
Esempio n. 13
0
        /*
         * note que quando vou buscar um objeto eles já estão instanciados.
         *
         * A intenção desse padrão é usar compartilhamento para suportar um grande número de objetos semelhantes de forma eficiente. O padrão visa minimizar o uso
         * de memória no armazenamento de vários objetos através do compartilhamento das informações em comum que essas instâncias possuem.
         *
         * Participantes:
         * Flyweight - é a interface que define como devem ser as classes que trabalham neste padrão. é importante citar que ela descreve como os dados extrínsecos são objtidos,
         * ou seja, as operações que fazem essa transposição de informações;
         * ConcreteFlyweight - são as classes que implementam o contrato IFlyweight e que devem permitir o comportamento. Essas classes mantém dados intrínsecos;
         * UnsharedConcreteFlyweight - possuem as mesmas características do ConcreteFlyweight, no entanto não são objetos compartilhados. Isso porque este padrão permite
         * o compartilhamento, mas não obriga que isso seja sempre seguido;
         * FlyweightFactory - Classe que é responsável pela criação dos objetos, além de manter o repositório de objetos que implementam o Flyweight;
         * Client - É quem utiliza os objetos IFlyweight, sempre os obtendo através do FlyweightFactory
         */
        public void Flyweight()
        {
            // externo
            int ext = 10;

            FlyweightFactory fabrica = new FlyweightFactory();

            Flyweight.Flyweight f1 = fabrica.getFlyweight("A");
            Response.Write(f1.Operation(ext++));

            Flyweight.Flyweight f2 = fabrica.getFlyweight("B");
            Response.Write("<br>" + f2.Operation(ext++));

            Flyweight.Flyweight f3 = fabrica.getFlyweight("C");
            Response.Write("<br>" + f3.Operation(ext++));

            Flyweight.Flyweight f4 = fabrica.getFlyweight("A");
            Response.Write("<br>" + f4.Operation(ext++));

            Flyweight.Flyweight f5 = new UnsharedConcreteFlyweight();
            Response.Write("<br>" + f5.Operation(ext++));
        }
        public void StructuralTest()
        {
            int extrinsicstate = 22;

            FlyweightFactory factory = new FlyweightFactory();

            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);
        }
Esempio n. 15
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);
        }
Esempio n. 16
0
        public static void TestFlyweight()
        {
            var extrinsicstate = 22;

            var factory = new FlyweightFactory();

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

            StringAssert.AreEqualIgnoringCase(fx.Operation(--extrinsicstate, true), "ConcreteFlyweight: " + extrinsicstate);

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

            StringAssert.AreEqualIgnoringCase(fy.Operation(--extrinsicstate, true), "ConcreteFlyweight: " + extrinsicstate);

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

            StringAssert.AreEqualIgnoringCase(fz.Operation(--extrinsicstate, true), "ConcreteFlyweight: " + extrinsicstate);

            var fu = new
                     UnsharedConcreteFlyweight();

            StringAssert.AreEqualIgnoringCase(fu.Operation(--extrinsicstate, true), "UnsharedConcreteFlyweight: " + extrinsicstate);
        }
Esempio n. 17
0
        private static void ShowFlyweight()
        {
            Console.WriteLine("================================================");
            Console.WriteLine("Pattern code (Flyweight):");
            int extrinsicstate       = 22;
            FlyweightFactory factory = new FlyweightFactory();
            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);
            Console.WriteLine("\nReal code (Flyweight):");
            // Build a document with text
            string document = "AAZZBBZB";

            char[]           chars            = document.ToCharArray();
            CharacterFactory characterFactory = new CharacterFactory();

            // 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);
            }
        }
Esempio n. 18
0
            private static void Test()
            {
                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");

                fz.Operation(--extrinsicstate);

                var fu = new UnsharedConcreteFlyweight();

                fu.Operation(--extrinsicstate);

                Console.ReadKey();
            }
Esempio n. 19
0
    private void Start()
    {
        // Arbitrary extrinsic state(外部状态)
        int externalState = 22;

        FlyweightFactory factory = new FlyweightFactory();

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

        fx.Operation(--externalState);

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

        fy.Operation(--externalState);

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

        fz.Operation(--externalState);

        UnsharedConcreteFlyweight fu = new UnsharedConcreteFlyweight();

        fu.Operation(--externalState);
    }
Esempio n. 20
0
        /// <summary>
        /// Client
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            #region Adapter

            Console.WriteLine("Adapter initialize ");

            Target target  = new Target();
            Target target2 = new Adaptee();

            Target target3 = new Adapter.Adapter();
            target.Request();
            Console.ReadKey();

            Console.WriteLine("Adapter Finalize " + Environment.NewLine);

            #endregion

            #region Bridge

            Console.WriteLine("Bridge initialize ");

            //Step 1
            ExportationBridge exp = new ExportationDOC();
            exp.Export();

            //Apply Brige Pattern
            Exportation exportacao = new ExportationEx();

            //Inject object
            exportacao.Implementor = new ExportationPDF();
            exportacao.Export();

            Console.WriteLine("Bridge finalize" + Environment.NewLine);
            Console.ReadKey();

            #endregion

            #region Composite

            Console.WriteLine("Composite initialize");

            var form = new Form("Cadastro Clientes");
            form.Add(new Button("Incluir"));
            form.Add(new Button("Consultar"));
            form.Add(new TextBox("Nome"));
            form.Add(new TextBox("Fone"));
            form.Display();

            Console.WriteLine("Composite finalize" + Environment.NewLine);
            Console.ReadKey();

            #endregion

            #region Decorator

            Console.WriteLine("Decorator initialize");

            //Step 1
            var dec = new DataSet();
            dec.Write();

            //Apply Decorator
            DataSet c = new DataSet();
            DataSetConcreteDecorator d = new DataSetConcreteDecorator();

            //Setando objeto a ser decorado
            d.setDatasetbase(c);
            d.Write();
            d.WriteXML();

            Console.WriteLine("Decorator finalize" + Environment.NewLine);
            Console.ReadKey();

            #endregion

            #region Facade

            Console.WriteLine("Facade initialize");

            //Step 1
            var email = new Mail(new SMTPSettings());
            var msg   = new MailMessage(new MailFormatTXT());
            msg.Message = "Hello world";
            email.Send(msg);
            Console.ReadKey();

            //Apply Facade pattern
            var emailFachada = new Email();
            emailFachada.Send("Hello world");

            Console.WriteLine("Facade finalize");
            Console.ReadKey();

            #endregion

            #region Flyweight

            Console.WriteLine("Flyweight initialize");

            int ext = 10;

            FlyweightFactory  factory = new FlyweightFactory();
            FlyweightAbstract f1      = factory.GetFlyweight("A");
            f1.Operation(ext++);
            FlyweightAbstract f2 = factory.GetFlyweight("B");
            f2.Operation(ext++);
            FlyweightAbstract f3 = factory.GetFlyweight("C");
            f3.Operation(ext++);
            FlyweightAbstract f4 = new UnsharedConcreteFlyweight();
            f4.Operation(ext++);

            Console.WriteLine("Flyweight finalize" + Environment.NewLine);
            Console.ReadKey();

            #endregion

            #region Proxy

            Console.WriteLine("Proxy initialize");

            //Step 1
            var calc = new Calc();
            var r    = calc.Somar(3, 5);
            Console.WriteLine(r.ToString());
            Console.ReadLine();

            //Aplly Proxy Pattern
            var calcProxy = new CalcProxy();
            var r2        = calcProxy.Somar(3, 5);
            Console.WriteLine(r2.ToString());

            Console.WriteLine("Proxy finalize" + Environment.NewLine);
            Console.ReadLine();

            #endregion
        }