Beispiel #1
0
        static void Main(string[] args)
        {
            CardFactory factory1 = new MoneyBackFactory(5000, 10);
            CardFactory factory2 = new TitaniumFactory(5000, 10);

            CreditCard creditCard1 = factory1.GetCreditCard();

            Console.WriteLine("\nYour card details are below : \n");
            Console.WriteLine("Card Type: {0}\nCredit Limit: {1}\nAnnual Charge: {2}",
                              creditCard1.CardType, creditCard1.CreditLimit, creditCard1.AnnualCharge);

            creditCard1 = factory2.GetCreditCard();

            Console.WriteLine("\nYour card details are below : \n");
            Console.WriteLine("Card Type: {0}\nCredit Limit: {1}\nAnnual Charge: {2}",
                              creditCard1.CardType, creditCard1.CreditLimit, creditCard1.AnnualCharge);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Factory Pattern");
            Console.WriteLine("The factory design pattern in C# is used to replace class constructors, abstracting the process of object generation so that the type of the object instantiated can be determined at run-time");
            Console.WriteLine();

            // Abstract implementation
            Console.WriteLine("Abstract implementation");

            Creator creator = new ConcreteCreator(); // You can not instantiate abstract classes
            Product product = creator.factoryMethod();

            product.Operation();

            Console.WriteLine();

            // Example implementation
            Console.WriteLine("Example implementation (credit cards)");

            CardFactory cardFactory;
            CreditCard  creditCard;

            cardFactory = new MoneyBackFactory(50000, 0);
            creditCard  = cardFactory.GetCreditCard();
            creditCard.PrintDetails();

            cardFactory = new TitaniumFactory(100000, 500);
            creditCard  = cardFactory.GetCreditCard();
            creditCard.PrintDetails();

            cardFactory = new PlatinumFactory(500000, 1000);
            creditCard  = cardFactory.GetCreditCard();
            creditCard.PrintDetails();

            Console.WriteLine();

            Console.ReadKey();
        }