Ejemplo n.º 1
0
    public static void DecoratorPatternDemo()
    {
        // when we want to decorate some class, we donot make any changes on the class, instead we use decorator interface to decorate the class
        Patterns.StructuralPatterns.DecoratorPattern.Shape circle = new Patterns.StructuralPatterns.DecoratorPattern.Circle();

        Patterns.StructuralPatterns.DecoratorPattern.ShapeDecorator shapeDecorator = new RedShapeDecorator(circle);
        shapeDecorator.Draw();
    }
Ejemplo n.º 2
0
        public static void DecoratorExample()
        {
            var circle       = new Circle();
            var redCircle    = new RedShapeDecorator(new Circle());
            var redRectangle = new RedShapeDecorator(new Rectangle());

            circle.Draw();
            redCircle.Draw();
            redRectangle.Draw();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            IShape circle       = new Circle();
            IShape redCircle    = new RedShapeDecorator(new Circle());
            IShape redRectangle = new RedShapeDecorator(new Rectangle());

            circle.draw();
            redCircle.draw();
            redRectangle.draw();
        }
Ejemplo n.º 4
0
 private static void Decorator()
 {
     Decorator.Shape circle       = new Decorator.Circle();
     Decorator.Shape redCircle    = new RedShapeDecorator(new Decorator.Circle());
     Decorator.Shape redRectangle = new RedShapeDecorator(new Decorator.Rectangle());
     Console.WriteLine("Circle with normal border");
     circle.draw();
     Console.WriteLine("\nCircle of red border");
     redCircle.draw();
     Console.WriteLine("\nRectangle of red border");
     redRectangle.draw();
 }
Ejemplo n.º 5
0
    static public void Main()
    {
        IShape circle = new Circle();
        //IShape rectangle = new Rectangle();
        IShape redCirle = new RedShapeDecorator(circle);

        // IShape redRectangle = new RedShapeDecorator (rectangle);

        //  circle.drawing();
        // rectangle.drawing();
        redCirle.drawing();
        //  redRectangle.drawing();
    }
        public static void Run()
        {
            IDrawable redCircle     = new RedShapeDecorator(new Circle());
            IDrawable blueRectangle = new BlueShapeDecorator(new Rectangle());

            Console.WriteLine("<Decorator Pattern Example>");
            Console.WriteLine();
            Console.Write("\t");
            redCircle.Draw();
            Console.Write("\t");
            blueRectangle.Draw();
            Console.WriteLine();
            Console.WriteLine("</Decorator Pattern Example>");
            Console.WriteLine();
        }
Ejemplo n.º 7
0
        public void PrintRedRectangleName()
        {
            //Arrange
            SUT = new RedShapeDecoratorBuilder().WithRectangle();

            var expected  = $"Shape: {nameof(Rectangle)}";
            var expected2 = $"Border Color: Red";

            //Act
            SUT.draw();

            //Assert: 1 Logical assertion
            Assert.IsTrue(Redirector.ToString().Contains(expected));
            Assert.IsTrue(Redirector.ToString().Contains(expected2));
        }
Ejemplo n.º 8
0
    public void Main()
    {
        IShape         circle       = new Circle();
        ShapeDecorator redCircle    = new RedShapeDecorator(new Circle());
        ShapeDecorator redRectangle = new RedShapeDecorator(new Rectangle());

        Console.WriteLine("Circle with normal border");
        circle.Draw();

        Console.WriteLine("Circle of red border");
        redCircle.Draw();

        Console.WriteLine("Rectangle of red border");
        redRectangle.Draw();
    }
Ejemplo n.º 9
0
        public void DemoDecoratorPattern()
        {
            Console.WriteLine("-----------------Decorator Pattern Example-----------------");
            IShape circle       = new Circle();
            IShape redCircle    = new RedShapeDecorator(new Circle());
            IShape redRectangle = new RedShapeDecorator(new DesingPattersExample.StructuralPatterns.DecoratorPattern.Rectangle());

            Console.WriteLine("Circle with normal border");
            circle.draw();

            Console.WriteLine("Circle of red border");
            redCircle.draw();

            Console.WriteLine("Rectangle of red border");
            redRectangle.draw();
        }
Ejemplo n.º 10
0
        public void Start()
        {
            var circle    = new Circle();
            var redCircle = new RedShapeDecorator(circle);

            var redRectangle = new RedShapeDecorator(new Rectangle());

            logger.Info("Circle with normal border.");
            circle.Draw();

            logger.Info("Circle of red border.");
            redCircle.Draw();

            logger.Info("Rectangle of red border.");
            redRectangle.Draw();
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            System.Console.WriteLine("Plain Circle Object");
            IShape circle = new Circle();

            circle.Draw();

            System.Console.WriteLine("\n\nDecorated Circle Object");
            IShape redCircle = new RedShapeDecorator(circle);

            redCircle.Draw();

            System.Console.WriteLine("\n\nDecorated Rectangle Object");
            IShape redRectangle = new RedShapeDecorator(new Rectangle());

            redRectangle.Draw();
        }
        public static void Run()
        {
            Console.WriteLine(typeof(_10_DecoratorPattern).GetClassName());

            IShape circle = new Circle();

            IShape redCircle = new RedShapeDecorator(new Circle());

            IShape redRectangle = new RedShapeDecorator(new Rectangle());

            Console.WriteLine("Circle with normal border");
            circle.Draw();

            Console.WriteLine("\nCircle of red border");
            redCircle.Draw();

            Console.WriteLine("\nRectangle of red border");
            redRectangle.Draw();

            Console.WriteLine();
        }
Ejemplo n.º 13
0
        static void Main(string[] args)
        {
            Shape circle = new Circle();

            RedShapeDecorator redShapeCircle = new RedShapeDecorator(circle);

            //Print a circle with normal border
            circle.Draw();
            //Print a circle with an additional red border
            redShapeCircle.Draw();


            Shape             rectangle          = new Rectangle();
            RedShapeDecorator redShapedRectangle = new RedShapeDecorator(rectangle);


            //Print a rectangle wth normal border
            rectangle.Draw();

            //Print a rectangle with an additional red border
            redShapedRectangle.Draw();
        }
Ejemplo n.º 14
0
        static void Main(string[] args)
        {
            IShape circle = new Circle();

            IShape rectangle = new Rectangle();

            IShape redCircle = new RedShapeDecorator(new Circle());

            IShape redRectangle = new RedShapeDecorator(new Rectangle());

            Console.WriteLine("Circle with normal border");
            circle.Draw();

            Console.WriteLine("\nCircle with red border");
            redCircle.Draw();

            Console.WriteLine("\nRectangle with normal border");
            rectangle.Draw();

            Console.WriteLine("\nRectangle with red border");
            redRectangle.Draw();
        }
Ejemplo n.º 15
0
        static void DecoratorTest()
        {
            Decorator.IShape circle       = new Decorator.Circle();
            Decorator.IShape redCircle    = new RedShapeDecorator(new Decorator.Circle());
            Decorator.IShape blueCircle   = new BlueShapeDecorator(new Decorator.Circle());
            Decorator.IShape redRectangle = new RedShapeDecorator(new Decorator.Rectangle());

            Decorator.IShape redBlueCircle = new RedShapeDecorator(new BlueShapeDecorator(new Decorator.Circle()));


            Console.WriteLine("Circle with normal border");
            circle.draw();


            Console.WriteLine("\nCircle of red border");
            redCircle.draw();

            Console.WriteLine("\nRectangle of red border");
            redRectangle.draw();

            Console.WriteLine("\nCircle of red and blue border ");
            redBlueCircle.draw();
        }
 public RedShapeDecoratorBuilder WithRectangle()
 {
     SUT = new RedShapeDecorator(new Rectangle());
     return(this);
 }
Ejemplo n.º 17
0
        static void Main(string[] args)
        {
            Console.WriteLine("Singleton Pattern");

            // Singleton Pattern
            SingleObject singleObject = SingleObject.Instance();

            singleObject.ShowMessage();

            Console.WriteLine("\nBuilder Pattern");

            // Builder Pattern
            MealBuilder mealBuilder = new MealBuilder();

            Meal vegMeal = mealBuilder.PrepareVegMeal();

            Console.WriteLine("Veg Meal");
            vegMeal.ShowItems();
            Console.WriteLine("Total Cost : " + vegMeal.GetCost());

            Meal nonVegMeal = mealBuilder.PrepareNonVegMeal();

            Console.WriteLine("NonVeg Meal");
            nonVegMeal.ShowItems();
            Console.WriteLine("Total Cost : " + nonVegMeal.GetCost());

            Console.WriteLine("\nAbstract Factory");

            // Abstract Factory Pattern
            AbstractFactory shapeFactory = FactoryProducer.GetFactory("shape");
            IShape          circleShape  = shapeFactory.GetShape("circle");

            circleShape.Draw();

            AbstractFactory colorFactory = FactoryProducer.GetFactory("color");
            IColor          blueColor    = colorFactory.GetColor("blue");

            blueColor.Fill();

            Console.WriteLine("\nAdapter");

            //Adapter Pattern
            AudioPlayer audioPlayer = new AudioPlayer();

            audioPlayer.Play("mp4");
            audioPlayer.Play("flac");
            audioPlayer.Play("vlc");

            Console.WriteLine("\nFacade");

            //Facade Pattern
            ShapeMaker shapeMaker = new ShapeMaker();

            shapeMaker.DrawCircle();
            shapeMaker.DrawRectangle();
            shapeMaker.DrawSquare();

            Console.WriteLine("\nDecorator doesn't work");

            //Decorator Pattern
            IDecoratorShape rectangle    = new RectangleDecorator();
            IDecoratorShape redRectangle = new RedShapeDecorator(new RectangleDecorator());

            Console.WriteLine("Rectangle with no color");
            rectangle.Draw();
            Console.WriteLine("Rectangle with color");
            redRectangle.Draw();

            Console.WriteLine("\nComposite");

            //Composite Pattern
            Employee DSI           = new Employee("employee1", "DSI", 100000);
            Employee chefDeProjet1 = new Employee("employee2", "Chef de projet", 60000);
            Employee chefDeProjet2 = new Employee("employee3", "Chef de projet", 60000);
            Employee dev1          = new Employee("employee4", "Développeur", 40000);
            Employee dev2          = new Employee("employee5", "Développeur", 40000);

            DSI.Add(chefDeProjet1);
            DSI.Add(chefDeProjet2);
            chefDeProjet1.Add(dev1);
            chefDeProjet2.Add(dev2);

            Console.WriteLine(DSI.Details());
            foreach (Employee e1 in DSI.GetSubordinates())
            {
                Console.WriteLine(e1.Details());
                foreach (Employee e2 in e1.GetSubordinates())
                {
                    Console.WriteLine(e2.Details());
                }
            }

            Console.WriteLine("\nBridge");

            //Bridge Pattern
            BridgeShape shape1 = new BridgeCircle(10, 10, 1, new GreenCircle());
            BridgeShape shape2 = new BridgeCircle(100, 100, 10, new RedCircle());

            shape1.Draw();
            shape2.Draw();

            Console.WriteLine("\nCommand");

            //Command Pattern
            Stock    stock1   = new Stock("laptop", 100);
            BuyStock buyStock = new BuyStock(stock1);

            Stock     stock2    = new Stock("screen", 30);
            SellStock sellStock = new SellStock(stock2);

            Broker broker = new Broker();

            broker.TakeOrder(buyStock);
            broker.TakeOrder(sellStock);
            broker.PlaceOrders();

            Console.WriteLine("\nInterpreter");

            //Interpreter Pattern
            IExpression isMale    = InterpreterPatternDemo.GetMaleExpression();
            IExpression isMarried = InterpreterPatternDemo.GetMarriedWomanExpression();

            Console.WriteLine("John is male ? " + isMale.Interpret("john"));
            Console.WriteLine("Barbara is male ? " + isMale.Interpret("barbara"));

            Console.WriteLine("Julie is married ? " + isMarried.Interpret("julie married"));
            Console.WriteLine("Bob is married ? " + isMarried.Interpret("bob married"));
        }
Ejemplo n.º 18
0
        static void Main(string[] args)
        {
            string _string;
            int    _shapeType     = 0;
            bool   _boorderAdded  = false;
            bool   _colorAdded    = false;
            bool   _bold          = false;
            IShape _circle        = new Circle();
            IShape _rectangle     = new Rectangle();
            IShape _redRectangle  = null;
            IShape _redCircle     = null;
            IShape _doubleArea    = null;
            IShape _boldRectangle = null;
            IShape _boldCircle    = null;
            IShape _doubleSize    = null;

            Console.WriteLine("Do you wish to create a Circle or Rectangle");
            Console.WriteLine("Type 'C' for Circle and 'R' for Rectangle ");
            _string = Console.ReadLine();

            ///WHICH shape
            do
            {
                if (_string == "R")
                {
                    _shapeType = 1;
                }
                else if (_string == "C")
                {
                    _shapeType = 2;
                }
                else
                {
                    Console.WriteLine("Please choose a real pattern");
                    _string = Console.ReadLine();
                }
            } while (_string != "R" && _string != "C");

            ///WISH RED BORDER OR NOT
            Console.WriteLine("");
            Console.WriteLine("Do you wish to add a red border to your shape");
            Console.WriteLine("Press 'Y' for yes or 'N' for no");
            _string = Console.ReadLine();
            if (_string == "Y" && _shapeType == 1)
            {
                _redRectangle = new RedShapeDecorator(_rectangle);
                _colorAdded   = true;
                _redRectangle.draw();
                Console.WriteLine("Default area is: {0}", _redRectangle.CalcArea());
            }
            else if (_string == "Y" && _shapeType == 2)
            {
                _redCircle  = new RedShapeDecorator(_circle);
                _colorAdded = true;
                _redCircle.draw();
                Console.WriteLine("Default area is: {0}", _redCircle.CalcArea());
            }
            else
            {
                Console.WriteLine("No red border added");
                if (_shapeType == 1)
                {
                    _rectangle.draw();
                    Console.WriteLine("Default area is: {0}", _rectangle.CalcArea());
                    _colorAdded = false;
                }
                else
                {
                    _circle.draw();
                    Console.WriteLine("Default area is: {0}", _circle.CalcArea());
                    _colorAdded = false;
                }
            }


            //WISH BOLD LINES OR NOT

            Console.WriteLine("Do you wish to make lines bold");
            Console.WriteLine("Press 'Y' for yes or 'N' for no");
            _string = Console.ReadLine();
            if (_string == "Y" && _shapeType == 1 && _colorAdded == true)
            {
                _boldRectangle = new BoldStyleDecorator(_redRectangle);
                _boldRectangle.draw();
                _bold = true;
                Console.WriteLine("Area with bold style: {0}", _boldRectangle.CalcArea());
            }
            else if (_string == "Y" && _shapeType == 2 && _colorAdded == true)
            {
                _boldCircle = new BoldStyleDecorator(_redCircle);
                _boldCircle.draw();
                _bold = true;
                Console.WriteLine("Area with bold style: {0}", _boldCircle.CalcArea());
            }
            else if (_string == "Y" && _shapeType == 1 && _colorAdded == false)
            {
                _boldRectangle = new BoldStyleDecorator(_rectangle);
                _boldRectangle.draw();
                Console.WriteLine("Area with bold style: {0}", _boldRectangle.CalcArea());
                _bold = true;
            }
            else if (_string == "Y" && _shapeType == 2 && _colorAdded == false)
            {
                _boldCircle = new BoldStyleDecorator(_circle);
                _boldCircle.draw();
                Console.WriteLine("Area with bold style: {0}", _boldCircle.CalcArea());
                _bold = true;
            }
            else
            {
                _bold = false;
                Console.WriteLine("No bold style added");
            }

            ///increase are by two
            ///
            Console.WriteLine("");
            Console.WriteLine("Do you wish to double the size Y/N");
            _string = Console.ReadLine();
            if (_string == "Y")
            {
                if (_shapeType == 1 && _bold == true)
                {
                    Console.WriteLine("");
                    Console.WriteLine("");
                    _doubleSize = new DoubleSizeDecorator(_boldRectangle);
                    Console.WriteLine("Area with bold style: {0}", _doubleSize.CalcArea());
                }
                else if (_shapeType == 2 && _bold == true)
                {
                    Console.WriteLine("");
                    Console.WriteLine("");
                    _doubleSize = new DoubleSizeDecorator(_boldCircle);
                    Console.WriteLine("Area with bold style: {0}", _doubleSize.CalcArea());
                }
                else if (_shapeType == 1 && _colorAdded == false && _bold == false)
                {
                    Console.WriteLine("");
                    Console.WriteLine("");
                    _doubleSize = new DoubleSizeDecorator(_rectangle);
                    Console.WriteLine("Area with bold style: {0}", _doubleSize.CalcArea());
                }
                else
                {
                    Console.WriteLine("");
                    Console.WriteLine("");
                    _doubleSize = new DoubleSizeDecorator(_circle);
                    Console.WriteLine("Area with bold style: {0}", _doubleSize.CalcArea());
                }
            }
            else
            {
                Console.WriteLine("Didn't increase size by two");
            }
        }
Ejemplo n.º 19
0
 public void TearDown()
 {
     Redirector.Dispose();
     SUT = null;
 }
 public RedShapeDecoratorBuilder WithCircle()
 {
     SUT = new RedShapeDecorator(new Circle());
     return(this);
 }
Ejemplo n.º 21
0
        static void Main(string[] args)
        {
            #region Exemplo 1 - Decorator
            ////Imposto iss = new ISS(new ICMS()); = 80
            //Imposto iss = new ISS();

            //Orcamento orcamento = new Orcamento(500);

            //double valor = iss.Calcula(orcamento);

            //Console.WriteLine(valor);
            #endregion

            #region Exemplo 2 - Decorator
            Orcamento orcamento         = new Orcamento(1000);
            Imposto   impostoAlto       = new ImpostoMuitoAlto(new ISS(new ICMS()));
            double    valorTotalImposto = impostoAlto.Calcula(orcamento);

            Console.WriteLine(valorTotalImposto);
            #endregion

            #region Exemplo 3 - Decorator e Template Method
            //Orcamento orcamento = new Orcamento(1000);
            //orcamento.AdicionarItem(new Item("Xiaomi Mi A2", 1000));

            //Imposto imposto = new ICMS(new ICPP(new IKCV(new ISS())));

            //double valorImposto = imposto.Calcula(orcamento);
            //Console.WriteLine(valorImposto);
            #endregion

            #region Exemplo 4 - Contas
            //List<Conta> listaDeContas = new List<Conta>();
            //listaDeContas.Add(new Conta(90, DateTime.Now.AddDays(-5)));
            //listaDeContas.Add(new Conta(501000, DateTime.Now));
            //listaDeContas.Add(new Conta(10000, DateTime.Now.AddDays(-7)));
            //listaDeContas.Add(new Conta(25000, DateTime.Now.AddMonths(-1)));

            //Filtro filtro = new FiltroContaSaldoMenor100Reais(new FiltroContaSaldoMaior500MilReais(new FiltroContaDataAberturaMesCorrente()));


            //var resultadoFiltrado = filtro.Filtrar(listaDeContas);

            //foreach (Conta item in resultadoFiltrado)
            //{
            //    Console.WriteLine(item);
            //}
            #endregion

            #region Exemplo Tutorials Point
            IShape circle = new Circle();

            IShape redCircle = new RedShapeDecorator(new Circle());

            IShape redRectangle = new RedShapeDecorator(new Rectangle());

            Console.WriteLine("Circle with normal border");         // Output:  Circle with normal border
            circle.Draw();                                          //          Shape: Circle

            Console.WriteLine("Circle of red border");              // Output:  Circle of red border
            redCircle.Draw();                                       //          Shape: Circle
                                                                    //          Border Color: Red

            Console.WriteLine("Rectangle of red border");           // Output:  Rectangle of red border
            redRectangle.Draw();                                    //          Shape: Rectangle
                                                                    //          Border Color: Red
            #endregion

            Console.ReadKey();
        }