コード例 #1
0
ファイル: Program.cs プロジェクト: brockdunda/CSharp-Projects
        static void Main(string[] args)
        {
            ComputerFactory factory = null;

            if (args.Length > 0 && args[0] == "BrandX")
            {
                factory = new BrandXFactory();
            }
            else
            {
                factory = new ConcreteComputerFactory();
            }

            new ComputerAssembler().AssembleComputer(factory);


            // Add in Product Factory
            var product = new Product();

            Console.WriteLine("Enter a product name: ");
            product.Name        = Console.ReadLine();
            product.Description = "A New Product";
            Console.WriteLine("Enter a price: ");
            product.Price = Convert.ToInt32(Console.ReadLine());

            // With Bank using BestForMe
            PaymentProcessor payment = new PaymentProcessor();

            payment.MakePayment(PaymentMethod.BEST_FOR_ME, product);

            // Try with PayPal has access to all the payment methods
            PaymentProcessor2 paypal = new PaymentProcessor2();

            paypal.MakePayment(PaymentMethod.PAYPAL, product);

            // Added BillDesk Payment
            PaymentProcessor2 payment2 = new PaymentProcessor2();

            payment2.MakePayment(PaymentMethod.BILL_DESK, product);

            Console.WriteLine(product.Name + ' ' + product.Price);

            Console.WriteLine("Now the Cars portion of the program.");
            Console.Read();

            // Cars
            var fordFiestaFactory = new FordFiestaFactory();
            var fordFiesta        = fordFiestaFactory.CreateCar("Blue");

            Console.WriteLine("Brand: {0} \nModel: {1} \nColor: {2}", fordFiesta.Make, fordFiesta.Model, fordFiesta.Color);
            Console.Read();

            Console.WriteLine("\n");
            ICarSupplier objCarSupplier = CarFactory.GetCarInstance(2);

            objCarSupplier.GetCarModel();
            Console.WriteLine(" and color is " + objCarSupplier.CarColor);
            Console.Read();
            Console.Read();
        } //Main
コード例 #2
0
        static void Main(string[] args)
        {
            ICarSupplier objCarSupplier = CarFactory.GetCarInstance(3);

            objCarSupplier.GetCarModel();
            Console.WriteLine("And Coloar is " + objCarSupplier.CarColor);

            Console.ReadLine();
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: SaReZCodes/DesignPatterns
        static void Main(string[] args)
        {
            CarFactory   carFactory   = new CarFactory();
            ICarSupplier carSuppllier = carFactory.GetCar(CarFactory.CarType.Peugeot);

            carSuppllier.Start();

            carSuppllier = carFactory.GetCar(CarFactory.CarType.Benz);
            carSuppllier.Start();

            carSuppllier = carFactory.GetCar(CarFactory.CarType.Bmw);
            carSuppllier.Start();
        }
コード例 #4
0
        public void BeginShowCase()
        {
            ICarSupplier objCarSupplier = CarFactory.GetCarInstance((int)Carmodels.Audi);

            objCarSupplier.GetCarModel();
            Console.WriteLine("And the color is " + objCarSupplier.CarColor);

            objCarSupplier = CarFactory.GetCarInstance((int)Carmodels.Bmw);
            objCarSupplier.GetCarModel();
            Console.WriteLine("And the color is " + objCarSupplier.CarColor);

            Console.ReadLine();
        }
コード例 #5
0
        /// <summary>
        /// The Factory method is a creational design pattern which provides an interface for creating
        /// objects without specifying their concrete classes.
        /// It defines a method which we can use to create an object instead of using its constructor.
        /// The important thing is that the subclasses can override this method and create objects of different types.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            ICarSupplier objCarSupplier = CarFactory.GetCarInstance(2);

            if (objCarSupplier != null)
            {
                objCarSupplier.GetCarModel();
                Console.WriteLine("And Coloar is " + objCarSupplier.CarColor);
            }
            else
            {
                Console.WriteLine("Car not exists");
            }


            Console.ReadLine();
        }
コード例 #6
0
        static void ExecuteProblem2Solution()
        {
            ICarSupplier cs1 = CarFactory.GetCarInstance(0);
            ICarSupplier cs2 = CarFactory.GetCarInstance(1);
            ICarSupplier cs3 = CarFactory.GetCarInstance(2);
            ICarSupplier cs4 = CarFactory.GetCarInstance(3);

            List <ICarSupplier> carSuppliers = new List <ICarSupplier> {
                cs1, cs2, cs3, cs4
            };

            carSuppliers.ForEach(x => {
                x.GetCarModel();
                Console.WriteLine("And Coloar is " + x.CarColor);
                Console.WriteLine();
            });
        }
コード例 #7
0
        /// <summary>
        ///     Main method
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            #region Singleton Pattern
            Console.WriteLine("--------------------------Singleton Pattern--------------------------");
            ThreadSafePrinter firstPrinterObject  = null;
            ThreadSafePrinter secondPrinterObject = null;
            ThreadSafePrinter thirdPrinterObject  = null;
            Task task1 = Task.Factory.StartNew(() =>
            {
                firstPrinterObject = ThreadSafePrinter.GetThreadSafePrinterInstance(GlobalText.FirstLabel, GlobalText.FirstDocumentLabel);
            });
            Task task2 = Task.Factory.StartNew(() =>
            {
                secondPrinterObject = ThreadSafePrinter.GetThreadSafePrinterInstance(GlobalText.SecondLabel, GlobalText.SecondDocumentLabel);
            });
            Task task3 = Task.Factory.StartNew(() =>
            {
                thirdPrinterObject = ThreadSafePrinter.GetThreadSafePrinterInstance(GlobalText.ThirdLabel, GlobalText.ThirdDocumentLabel);
            });
            Task.WaitAll(task1, task2, task3);
            Console.WriteLine(GlobalText.AllThreadsCompleteText);
            Console.WriteLine(GlobalText.FirstPrinterAndSecondPrinterObjectSameText, firstPrinterObject.Equals(secondPrinterObject) ? "Yes" : "No");
            Console.WriteLine(GlobalText.FirstPrinterAndThirdPrinterObjectSameText, firstPrinterObject.Equals(thirdPrinterObject) ? "Yes" : "No");
            Console.WriteLine(GlobalText.SecondPrinterAndThirdPrinterObjectSameText, secondPrinterObject.Equals(thirdPrinterObject) ? "Yes" : "No");
            #endregion

            #region Factory Pattern
            Console.WriteLine("\r\n\r\n--------------------------Factory Pattern--------------------------");
            ICarSupplier objCarSupplier = CarFactory.GetCarInstance(3);
            objCarSupplier.GetCarModel();
            Console.WriteLine(GlobalText.AndColorIsText + objCarSupplier.CarColor);
            #endregion

            #region Abstract Factory Pattern
            Console.WriteLine("\r\n\r\n--------------------------Abstract Factory Pattern--------------------------");
            CarClient hondaClient;
            CarClient toyotaClient;
            Console.WriteLine(GlobalText.HondaCarFactoryText);
            hondaClient = new CarClient(new HondaFactory(), GlobalText.CompactText);
            Console.WriteLine(string.Format(GlobalText.ManufactureingCompactSedanText, hondaClient.GetManufacturedSedanName()));
            Console.WriteLine(string.Format(GlobalText.ManufactureingCompactSuvText, hondaClient.GetManufacturedSuvName()));
            hondaClient = new CarClient(new HondaFactory(), GlobalText.FullText);
            Console.WriteLine(string.Format(GlobalText.ManufactureingFullSedanText, hondaClient.GetManufacturedSedanName()));
            Console.WriteLine(string.Format(GlobalText.ManufactureingFullSuvText, hondaClient.GetManufacturedSuvName()));
            Console.WriteLine(GlobalText.ToyotaCarFactoryText);
            toyotaClient = new CarClient(new ToyotaFactory(), GlobalText.CompactText);
            Console.WriteLine(string.Format(GlobalText.ManufactureingCompactSedanText, toyotaClient.GetManufacturedSedanName()));
            Console.WriteLine(string.Format(GlobalText.ManufactureingCompactSuvText, toyotaClient.GetManufacturedSuvName()));
            toyotaClient = new CarClient(new ToyotaFactory(), GlobalText.FullText);
            Console.WriteLine(string.Format(GlobalText.ManufactureingFullSedanText, toyotaClient.GetManufacturedSedanName()));
            Console.WriteLine(string.Format(GlobalText.ManufactureingFullSuvText, toyotaClient.GetManufacturedSuvName()));
            #endregion

            #region Builder Pattern
            Console.WriteLine("\r\n\r\n--------------------------Builder Pattern--------------------------");
            DrinkMaker    maker;
            IDrinkBuilder builder;
            Console.WriteLine(GlobalText.TeaCoffeeSelectText);
            maker = new DrinkMaker();
            while (true)
            {
                string input = Console.ReadLine();
                if (input == "T")
                {
                    builder = new TeaBuilder();
                    maker.MakeDrink(builder);
                    Console.WriteLine(builder.Drink.ToString() + GlobalText.IsReadyText);
                    Console.WriteLine(GlobalText.TeaCoffeeSelectText);
                }
                else if (input == "C")
                {
                    builder = new CoffeeBuilder();
                    maker.MakeDrink(builder);
                    Console.WriteLine(builder.Drink.ToString() + GlobalText.IsReadyText);
                    Console.WriteLine(GlobalText.TeaCoffeeSelectText);
                }
                else
                {
                    break;
                }
            }
            #endregion

            #region Decorator Pattern
            Console.WriteLine("\r\n\r\n--------------------------Decorator Pattern--------------------------");
            //Step 1: Define some dishes, and how many of each we can make
            FreshSalad caesarSalad = new FreshSalad(GlobalText.CrispRomaineLettuceText, GlobalText.FreshlyGratedParmesanCheeseText, GlobalText.HouseMadeCaesarDressingText);
            caesarSalad.Display();
            Pasta fettuccineAlfredo = new Pasta(GlobalText.FreshMadeDailyPastaText, GlobalText.CreamlyGarlicAlfredoSauceText);
            fettuccineAlfredo.Display();
            Console.WriteLine(GlobalText.MakingTheseDishesAvailableText);
            //Step 2: Decorate the dishes; now if we attempt to order them once we're out of ingredients, we can notify the customer
            Available caesarAvailable  = new Available(caesarSalad, 3);
            Available alfredoAvailable = new Available(fettuccineAlfredo, 4);
            //Step 3: Order a bunch of dishes
            caesarAvailable.OrderItem("John");
            caesarAvailable.OrderItem("Sally");
            caesarAvailable.OrderItem("Manush");
            alfredoAvailable.OrderItem("Sally");
            alfredoAvailable.OrderItem("Francis");
            alfredoAvailable.OrderItem("Venkat");
            alfredoAvailable.OrderItem("Diana");
            alfredoAvailable.OrderItem("Dennis");
            caesarAvailable.Display();
            alfredoAvailable.Display();
            #endregion

            #region Facade Pattern
            Console.WriteLine("\r\n\r\n--------------------------Facade Pattern--------------------------");
            HomeFacade homeFacade = new HomeFacade();
            Console.WriteLine(GlobalText.LeaveHomeForOfficeControlText);
            homeFacade.LeaveHomeforOffice();
            Console.WriteLine(GlobalText.ArriveHomeFromOfficeControlText);
            homeFacade.ArriveHomefromOffice();
            Console.ReadLine();
            Console.ReadLine();
            #endregion
        }