Esempio n. 1
0
        private static void Main(string[] args)
        {
            //BUILDER PATTERN:
            //If you have a class with many options and a big constructor. Use builder pattern
            var burger = BurgerBuilder.Init(100)
                         .WithCheese()
                         .WithBacon()
                         .Build();

            //ADAPTER PATTERN:
            //An adapter pattern is used to combine 2 different classes
            //Scenerio: EmployeeManager can only return results in CSV, But we need in pipe seperated format
            IEmployeeManager adaptedInstance = new EmployeeAdapter();
            var pipeSeperated = adaptedInstance.GetEmployess();

            //FACADE PATTERN:
            //Facade pattern can be used for simplified entrys to sub systems (Many internal functions)
            var shoppingfacade = new ShoppingFacade();

            shoppingfacade.BuyItem();

            //DECORATOR PATTERN:
            //Usefull to extend new functions to a class without inheriting (or if sealed)
            var car             = new BMW();
            var discountedPrice = new DiscountCalculator(car).GetDiscountedPrice();

            //FACTORY PATTERN:
            //Factory pattern helps build objects so client no need to know all concreate classes
            IInvoice invoice1 = InvoiceFactory.GetInvoice(1);
            IInvoice invoice2 = InvoiceFactory.GetInvoice(2);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            // // Example exercise 1
            GameContext g = new GameContext(new DuringGame());

            g.SpaceshipHit(20);
            g.SpaceshipHit(20);
            g.SpaceshipHit(2);
            g.SpaceshipHit(15);
            g.SpaceshipHit(5);

            // Example exercise 2
            Composite root  = new Composite(2);
            Composite child = new Composite(2);

            child.addChild(new Leaf(2));
            root.addChild(child);
            root.addChild(new Leaf(2));
            root.addChild(new Leaf(8));
            Console.WriteLine(root.Sum());
            Console.WriteLine(isEvenRecursionExercise2(root));

            // // Example exercise 3
            Mercedes      c1 = new Mercedes();
            BMW           c2 = new BMW();
            CarController cc = new CarController(c1);

            c1.StartDriving();
            c1.TurnLeft();
            cc.car = c2;
            cc.TurnRight();
            cc.DisplayLocation();
            ProxyCarController pcc = new ProxyCarController(cc);

            pcc.DisplayLocation();

            // // Example exercise 5
            LogFactory.GetLogWritter(LogEnum.Info).WriteLog();
            LogFactory.GetLogWritter(LogEnum.Error).WriteLog();
            LogFactory.GetLogWritter(LogEnum.Fatal).WriteLog();

            // // Example exercise 6
            FlyWeightPower fwp = new FlyWeightPower();

            fwp.addPowerNumber(2, 3);
            fwp.addPowerNumber(3, 2);
            fwp.addPowerNumber(3, 3);
            fwp.addPowerNumber(11, 2);
            PowerNumberCalculator calc         = new PowerNumberCalculator();
            List <PowerNumber>    powerNumbers = calc.calculatePowerNumbers(fwp);

            powerNumbers.ForEach(x => Console.WriteLine(x.calculation));

            // Example exercise 7
            Chatroom    chat = new Chatroom();
            Participant p1   = new Participant(1);
            Participant p2   = new Participant(2);
            Participant p3   = new Participant(3);

            chat.addParticipant(p1);
            chat.addParticipant(p2);
            p1.SendAll("hello!");
            p1.SendToUser(2, "Wanna netflix and chill?");
            p2.SendAll("Lets all netflix and chill!");

            // Example exercise 8
            DeadLiftWorkoutBuilder deadliftBuilder = new DeadLiftWorkoutBuilder();

            deadliftBuilder.buildWorkout();
            SquatWorkoutBuilder sb = new SquatWorkoutBuilder();

            sb.buildWorkout();

            // // Example exercise 9
            CarMakerFactoryBase normal = new NormalCarFactory();

            normal.createAC();
            normal.createBatery();
            normal.createEngine();
            normal.createLightSystem();
            normal.createSeats();
            normal.createTin();
            normal.createWheels();

            CarMakerFactoryBase hybrid = new HybridCarFactory();

            hybrid.createAC();
            hybrid.createBatery();
            hybrid.createEngine();
            hybrid.createLightSystem();
            hybrid.createSeats();
            hybrid.createTin();
            hybrid.createWheels();

            // // Example exercise 10
            WindowHandler h1 = new WindowHandler();
            WindowHandler h2 = new Shadow();
            WindowHandler h3 = new TickingBackground();
            WindowHandler h4 = new D3Light();
            WindowHandler h5 = new ColorfulBorder();

            h1.setNext(h2);
            h2.setNext(h3);
            h3.setNext(h4);
            h4.setNext(h5);
            h1.handle();

            // // Example exercise 12
            OfficeComputerPackBuilder b1 = new OfficeComputerPackBuilder();

            b1.buildComputerPack();
            SalonComputerPackBulider b2 = new SalonComputerPackBulider();

            b2.buildComputerPack();
            GamingComputerPackBulider b3 = new GamingComputerPackBulider();

            b3.buildComputerPack();
        }