예제 #1
0
        private static void DecoratorScenario()
        {
            var economyCar = new EconomyCar();
            var economyCarWithDeluxeAccessories = new DeluxeAccessories(economyCar);

            var deluxeCar = new DeluxeCar();
            var deluxeCarWithBasicAccessories  = new BasicAccessories(deluxeCar);
            var deluxeCarWithDeluxeAccessories = new DeluxeAccessories(deluxeCar);

            Console.WriteLine($"{economyCar.GetDescription()} - {economyCar.GetCost()}");
            Console.WriteLine($"{economyCarWithDeluxeAccessories.GetDescription()} - {economyCarWithDeluxeAccessories.GetCost()}");
            Console.WriteLine($"{deluxeCar.GetDescription()} - {deluxeCar.GetCost()}");
            Console.WriteLine($"{deluxeCarWithBasicAccessories.GetDescription()} - {deluxeCarWithBasicAccessories.GetCost()}");
            Console.WriteLine($"{deluxeCarWithDeluxeAccessories.GetDescription()} - {deluxeCarWithDeluxeAccessories.GetCost()}");
        }
예제 #2
0
        public static void DecoratorDemo()
        {
            // Create EcomomyCar instance.
            ICar objCar = new EconomyCar();

            CarAccessoriesDecorator objAccessoriesDecorator = new AdvancedAccessories(objCar);

            objAccessoriesDecorator = new SportAccessories(objAccessoriesDecorator);

            Console.Write("Car Detials: " + objAccessoriesDecorator.GetDescription());
            Console.WriteLine("\n\n");
            Console.Write("Total Price: " + objAccessoriesDecorator.GetCost());

            Console.Read();
        }
예제 #3
0
        public static void DecoratorPattern()
        {
            //Create EcomomyCar instance.
            ICar objCar = new EconomyCar();

            //Wrp EconomyCar instance with BasicAccessories.
            CarAccessoriesDecorator objAccessoriesDecorator = new BasicAccessories(objCar);

            //Wrap EconomyCar instance with AdvancedAccessories instance.
            objAccessoriesDecorator = new AdvancedAccessories(objAccessoriesDecorator);

            Console.Write("Car Details: " + objAccessoriesDecorator.GetDescription());
            Console.WriteLine("\n\n");
            Console.Write("Total Price: " + objAccessoriesDecorator.GetCost());

            Console.Read();
        }
예제 #4
0
        // Types of Design Patterns
        //1. Creational: This type deals with the object creation and initialization. This pattern gives the
        // program more flexibiliy in deciding which objects need to be create for a given case
        // ex: singleton, factory, abstract factory.. etc
        //2. Structural: This type deals with class and object composition.
        // This pattern focuses on decoupling interface and implementation of classes and its objects.
        // ex: Adapter, Bridge.. etc
        //3. Behavioural : deals with communication b/w objects and classes ex: command, interpreter.. etc

        static void Main(string[] args)
        {
            // Creational Patterns
            //1. Singleton
            Parallel.Invoke(() => Test1Prnt(), () => Test2Prnt());

            // Structural patterns
            //1. Composite Pattern - Hierarchy, folder structures and menu bars
            IDirectReport dr1 = new DirectReport("Report1", "IT");
            IDirectReport dr2 = new DirectReport("Report2", "IT");
            IDirectReport dr3 = new DirectReport("Report3", "IT");
            IDirectReport dr4 = new DirectReport("Report4", "HR");
            IDirectReport dr5 = new DirectReport("Report5", "HR");

            IDirectReport mgr1 = new Manager("ManagerIT", "IT")
            {
                DirectReports = { dr1, dr2, dr3 }
            };
            IDirectReport mgr2 = new Manager("ManagerHR", "HR")
            {
                DirectReports = { dr4, dr5 }
            };

            IDirectReport mgrGlobal = new Manager("GlobalManager", "Global")
            {
                DirectReports = { mgr1, mgr2 }
            };

            //mgrGlobal.GetInfo();

            //2. Decorator Pattern - Used when the object needs to be extended without effecting the concrete implementation
            ICar car = new EconomyCar();
            //var cardecorator = new FinalOffer(car);
            //Console.WriteLine($"Make:{cardecorator.Make},OriginalPrice:{cardecorator.Price().ToString()}, " +
            //    $"AfterDiscount:{cardecorator.GetDiscount()}");

            //3. Adapter Pattern - Combine two incompatibe classes and interfaces using adapter pattern
            //Diff b/w decorator and adapter https://stackoverflow.com/questions/17731522/what-are-the-differences-between-the-decorator-and-adapter-patterns/17731655
            IEmployee emp = new EmployeeAdapter();

            //  var jsonData = emp.GetEmployees();
            //Console.WriteLine(jsonData);
            Console.ReadLine();
        }