コード例 #1
0
        static void Main(string[] args)
        {
            /*Problem - Usual Program execution */
            //Generally we will get the Card Type from UI.
            //Here we are hardcoded the card type
            string      cardType    = "MoneyBack";
            ICreditCard cardDetails = null;

            //Based of the CreditCard Type we are creating the
            //appropriate type instance using if else condition
            if (cardType == "MoneyBack")
            {
                cardDetails = new MoneyBack();
            }
            else if (cardType == "Titanium")
            {
                cardDetails = new Titanium();
            }
            else if (cardType == "Platinum")
            {
                cardDetails = new Platinum();
            }
            if (cardDetails != null)
            {
                Console.WriteLine("CardType : " + cardDetails.GetCardType());
                Console.WriteLine("CreditLimit : " + cardDetails.GetCreditLimit());
                Console.WriteLine("AnnualCharge :" + cardDetails.GetAnnualCharge());
            }
            else
            {
                Console.Write("Invalid Card Type");
            }
            /*Problem End here*/

            /*Solution - Implementing Factory pattern*/
            ICreditCard cardDetailsSolution = CreditCardFactoryPattern.GetCreditCard("Platinum");

            if (cardDetails != null)
            {
                Console.WriteLine("CardType : " + cardDetailsSolution.GetCardType());
                Console.WriteLine("CreditLimit : " + cardDetailsSolution.GetCreditLimit());
                Console.WriteLine("AnnualCharge :" + cardDetailsSolution.GetAnnualCharge());
            }
            else
            {
                Console.Write("Invalid Card Type");
            }
            /* Solution End Here*/

            Console.ReadLine();
        }
コード例 #2
0
        public static ICreditCard GetCreditCard(string cardType)
        {
            ICreditCard cardDetails = null;

            if (cardType == "MoneyBack")
            {
                cardDetails = new MoneyBack();
            }
            else if (cardType == "Titanium")
            {
                cardDetails = new Titanium();
            }
            else if (cardType == "Platinum")
            {
                cardDetails = new Platinum();
            }
            return(cardDetails);
        }
コード例 #3
0
        protected override ICreditCard MakeProduct()
        {
            ICreditCard product = new Platinum();

            return(product);
        }