- Factory method used to expose an interface with a method on it to create object of certain type. But Abstract factory – exposes an interface to create related objects : families of objects.
- To provide an interface for creating families of related or dependent objects without specifying their concrete classes.
- Clients are isolated from implementation classes:- decoupling
- Factory method :- produces one product. But Abstract factory produces families of products.
- Factory method creates objects through inheritence, but Abstract factory creates families of objects through composition.
- Closely related pattern to abstract factory method pattern
- Factory method
- Prototype
Discount related..
namespace simple_factory
{
public interface IDiscount
{
decimal Discount { get; }
}
internal class StateWiseMBBSCoachingDiscount : IDiscount
{
public decimal Discount => 2000;
}
//added newly
internal class CouponApplyMBBSCoachingDiscount : IDiscount
{
public decimal Discount => 1000;
}
}
Total Cost Related..
namespace simple_factory
{
public 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;
}
}
}
}
}
ABSTRACT FACTORY IMPLEMENTED....
using static simple_factory.StateWiseMBBSCoachingCost;
namespace simple_factory
{
public interface IMBBSCostDiscountFactory
{
ITotalCost CreateCostService();
IDiscount CreateDiscountService();
}
public class StateCoachingFinalCostFactory : IMBBSCostDiscountFactory
{
private readonly string _stateCode;
public StateCoachingFinalCostFactory(string stateCode)
{
_stateCode = stateCode;
}
public ITotalCost CreateCostService()
{
return new StateWiseMBBSCoachingCost(_stateCode);
}
public IDiscount CreateDiscountService()
{
return new StateWiseMBBSCoachingDiscount();
}
}
public class CouponApplyMBBSCoachingFinalCostFactory : IMBBSCostDiscountFactory
{
private readonly Guid _couponCode;
public CouponApplyMBBSCoachingFinalCostFactory(Guid couponCode)
{
_couponCode = couponCode;
}
public ITotalCost CreateCostService()
{
return new CouponApplyMBBSCoachingCost(_couponCode);
}
public IDiscount CreateDiscountService()
{
return new CouponApplyMBBSCoachingDiscount();
}
}
}
MbbsCoaching - Abstract Layer..
namespace simple_factory
{
public class MbbsCoaching
{
private readonly IDiscount _discountService;
private readonly ITotalCost _totalCostService;
public MbbsCoaching(IMBBSCostDiscountFactory factory)
{
_discountService = factory.CreateDiscountService();
_totalCostService = factory.CreateCostService();
}
public decimal FinalCost()
{
return _totalCostService.TotalCost - _discountService.Discount;
}
}
}
Program.cs
var factory = new CouponApplyMBBSCoachingFinalCostFactory(new Guid());
var mbbscoaching = new MbbsCoaching(factory);
Console.WriteLine(mbbscoaching.FinalCost());
OUTPUT
4000
(50000-1000)
Same outupt as previous article
Github url