コード例 #1
0
        public static void Test()
        {
            /*
             * Scenario: we've had a concrete credit card class and we needed new methods
             * for air travel and food supplies cashback calculation. Instead of adding to the interface,
             * and then implementing them in each sub-class, we simply defined only 1 method
             * in the CreditCard interface - accept() which takes on a visitor who supplies
             * the functionality we need. Thus, we had to violate the OCP in the CreditCard interface
             * and implementations **only** once, and have opened our hierarchy to future behavioral extensions.
             */

            var bronze = new BronzeCreditCard();
            var silver = new SilverCreditCard();
            var gold   = new GoldCreditCard();

            // we only need to create 1 visitor, which can interact with our 3 objects
            var airtraverlVisitor = new AirtravelOfferVisitor();

            bronze.Accept(airtraverlVisitor);
            silver.Accept(airtraverlVisitor);
            gold.Accept(airtraverlVisitor);

            var foodVisitor = new FoodOfferVisitor();

            gold.Accept(foodVisitor);
        }
コード例 #2
0
 public void VisitSilverCreditCard(SilverCreditCard card)
 {
     Console.WriteLine("The Silver credit card has 0.3% cashback for food supplies.");
 }
コード例 #3
0
 public void VisitSilverCreditCard(SilverCreditCard card)
 {
     Console.WriteLine("The Silver credit card has 2.5% cashback on air-travel purchases.");
 }