Factory Method Pattern

  • A factory – is an object for creating objects..
  • To define an interface for creating an object, but let subclasses decide which class to instantiate.
  • Factory method eliminates the need to bind application-specific classes to your code.
  • New types of your products can be added without breaking client code : open/closed principle.

       RELATED PATTERN:- 

  •  Abstract factory pattern
  • Prototype pattern
  • Template :- Factory methods are often called from within template methods
  • PrivilegedOutsideStateMBBSCoachingFactory – takes state code in the constructor
  • ApplyCouponCodeMBBSCoachingFactory – takes couponcode in the constructor
				
					namespace simple_factory
{
    interface ITotalCost
    {
        decimal TotalCost { get; }
    }

    internal class StateWiseMBBSCoachingCost : ITotalCost
    {
        private readonly string _stateCode;
        public StateWiseMBBSCoachingCost(string stateCode)
        {
            _stateCode = stateCode;
        }
        public decimal TotalCost
        {
            get
            {
                switch (_stateCode)
                {
                    case "WB":
                        return 25500;
                    case "MP":
                        return 35500;
                    case "Goa":
                        return 15500;
                    default:
                        return 20000;
                }
            }
        }
    }


    internal class CouponApplyMBBSCoachingCost : ITotalCost
    {
        private readonly Guid _couponCode;
        public CouponApplyMBBSCoachingCost(Guid couponCode)
        {
            _couponCode = couponCode;
        }
        public decimal TotalCost
        {
            get
            {
                //check for cup
                return 5000;
            }
        }
    }

    interface ITotalCostFactory
    {
        public ITotalCost CreateTotalCostService();
    }


    internal class CouponApplyMBBSCoachingCostFactory : ITotalCostFactory
    {
        private readonly Guid _couponCode;
        public CouponApplyMBBSCoachingCostFactory(Guid couponCode)
        {
            _couponCode = couponCode;
        }
        public ITotalCost CreateTotalCostService()
        {
            return new CouponApplyMBBSCoachingCost(_couponCode);
        }
    }

    internal class StateWiseMBBSCoachingCostFactory : ITotalCostFactory
    {
        private readonly string _stateCode;
        public StateWiseMBBSCoachingCostFactory(string stateCode)
        {
            _stateCode = stateCode;
        }
        public ITotalCost CreateTotalCostService()
        {
            return new StateWiseMBBSCoachingCost(_stateCode);
        }
    }
}



				
			
				
					// See https://aka.ms/new-console-template for more information
using simple_factory;


var factories = new List<IDiscountFactory>
{
    new PrivilegedOutsideStateMBBSCoachingFactory("WB"),
    new ApplyCouponCodeMBBSCoachingFactory(new Guid())
};

foreach (var fact in factories)
{
    var discount = fact.CreateDiscountService();
    Console.WriteLine(discount.DiscountPercentage);
}
				
			
				
					var ddownValue = "WB";
ITotalCostFactory factoryObj;
factoryObj = new StateWiseMBBSCoachingCostFactory(ddownValue);
Console.WriteLine(factoryObj.CreateTotalCostService().TotalCost);//25500
				
			

25500

Leave a Comment