コード例 #1
0
ファイル: Program.cs プロジェクト: bounaykut/DesignPatterns
    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();
    }
コード例 #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();
        }
コード例 #3
0
        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();
        }
コード例 #4
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();
    }
コード例 #5
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();
        }
コード例 #6
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();
        }
コード例 #7
0
        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();
        }
コード例 #8
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();
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: kgiff14/DesignPatterns
        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();
        }
コード例 #10
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();
        }
コード例 #11
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"));
        }