Bridge Pattern -one more example

  • Holds the reference of Icoupon
  • This acts as bridge.

public readonly Icoupon _coupon =null!;

				
					public abstract class Meals
    {
        
        public readonly Icoupon _coupon =null!;
        public Meals(Icoupon coupon)
        {
            _coupon = coupon;
        }
        public abstract decimal MealPrice();
    }

    public class VegMeals : Meals
    {
        public VegMeals(Icoupon coupon) :base(coupon)
        {

        }
        public override decimal MealPrice()
        {
            return 100 - 100 * _coupon.CouponValue/100;
        }
    }

    public class NonVegMeals : Meals
    {
        public NonVegMeals(Icoupon coupon) : base(coupon)
        {

        }
        public override decimal MealPrice()
        {
            return 150 - 150 * _coupon.CouponValue / 100;
        }
    }
				
			
				
					public interface Icoupon
    {
        int CouponValue { get; }
    }

    public class FivePercentageCoupon : Icoupon
    {
        public int CouponValue { get => 5; }
    }
    public class TenPercentageCoupon : Icoupon
    {
        public int CouponValue { get => 10; }
    }
				
			
				
					using bridgePattern;

var fivePercentageCoupon = new FivePercentageCoupon();
var vegmeals = new VegMeals(fivePercentageCoupon);
Console.WriteLine(vegmeals.MealPrice());//95

var tenPercentageCoupon = new TenPercentageCoupon();
var nonVegmeals = new NonVegMeals(tenPercentageCoupon);
Console.WriteLine(nonVegmeals.MealPrice());//135
				
			

Leave a Comment