রবিবার, ১৭ নভেম্বর, ২০১৩

Strategy Pattern



Intent:
  • Related Algorithm mg~‡ni family define K‡i Ges Dnv‡`i‡K encapsulate K‡i|
  • Strategy pattern G defined a algorithm mg~n condition Gi Dci base K‡i interchange nq|
  • A_©vr Strategy pattern use K‡i host ‡_‡K algorithm ‡K disassociate K‡i  Ges dynamically algorithms swap ability enable K‡i|
  • Strategy pattern algorithm mg~n‡K object AvKv‡i encapsulate K‡i|
  • Abstract or interface Øviv Client algorithm mg~n‡K refer K‡i| Abstract or interface Zv‡`i interchangeability enable K‡i|
UML Diagram:
  • bx‡Pi Figure Strategy pattern Gi UML representation K‡i-






  • Dc‡iv³ UML representation Gi class mg~‡ni roles wb¤œiƒc-
    • Different Calculation different ConcreteStrategy ‡Z iv‡L Ges ConcreteStrategy class mg~n abstract class or interface (IStrategy) ‡K implement K‡i|
    • Context class IStrategy Øviv ConcreteStrategy calculation mg~n defer K‡i|
    • IStrategy algorithm Gi Rb¨ GKwU interface
    • G&B instance G Bnv single calculate method contain K‡i|
    • ConcreteStrategy IStrategy Gi GKwU implementation.
Real life example:
GK¯’vb †_‡K Av‡iK ¯’v‡b hvIqvi Rb¨ transportation  Gi †ÿ‡Î Several options _vK‡Z cv‡i-
·         wbR¯^ car driving Gi gva¨‡g
·         Taxi cab hiring Gi gva¨‡g
·         City bus Gi gva¨‡g
·         Limousine service Gi gva¨‡g BZ¨vw`... ... ...
Traveler GK¯’vb †_‡K Av‡iK ¯’v‡b hvIqvi Dc‡iv³ options mg~n mn Av‡iv A‡bK option/strategy B e¨envi Ki‡Z cv‡i|
G‡ÿ‡Î mKj options/strategy mg~nB Traveler ‡Z Zvi destination G wb‡q hv‡e|
Traveling strategy selection Gi †ÿ‡Î Traveler ‡hmKj e¨vcvimg~n consideration G †i‡L Traveling strategy selection Ki‡e †m¸‡jv n‡jv-
·         Cost
·         Convenience
·         Time
myZivs Avgiv †`L‡Z cvw”Q †h KwZcq condition Gi Dci base K‡i Traveling strategy change n‡”Q †Zgwb
Strategy pattern G defined a algorithm mg~n condition Gi Dci base K‡i interchange nq|



Code Example:
  • Strategy pattern demonstrate Kivi Rb¨ Avgiv commerce shopping basket G applied discount process use Ki‡ev|
  • wb‡¤œv³ diagram G Avgv‡`i GB exercise Strategy pattern Gi mv‡_ involved class mg~‡ni demonstrate Kiv n‡jv|






  • StrategyPattern bv‡g GKwU solution create Kwi Ges Dnv‡Z
  • StrategyPattern.Model  bv‡g GKwU C# class library project add Kwi|
  • Basket bv‡g GKwU new class add Kwi Ges following code wjwL-
namespace StrategyPattern.Model
{
public class Basket
{
public decimal TotalCost { get; set; }
}
}
  • Dc‡iv³ Basket class G total basket cost hold Kivi Rb¨ TotalCost bv‡g GKwU property define Kiv n‡q‡Q-
  • GLb Avgiv discount strategies create Kivi Rb¨ IBasketDiscountStrategy bv‡g GKwU interface add Ki‡ev hvi contract wb¤§iƒc-
namespace StrategyPattern.Model
{
public interface IBasketDiscountStrategy
{
decimal GetTotalCostAfterApplyingDiscountTo(Basket basket);
}
}
  • interface G GetTotalCostAfterApplyingDiscountTo method argument wnmv‡e Basket object ‡bq |
  • Interface Gi GK GKwU implementation Avjv`v Avjv`v discount apply K‡i Ges basket price return K‡i (discount include K‡i).
  • First discount strategy  customer Gi Rb¨ ZLbB discount calculate Ki‡e hLb discount Gi Rb¨ vendor company cÖ`Ë †Kvb kZ© fulfill Ki‡e| wb‡¤œ †miKg K‡qKwU kZ© cÖ`Ë n‡jv-
  •  Total Price
    • $100 USD Gi †ekx n‡j $10 USD discount
    • $50 USD Gi †ekx n‡j $05 USD discount
    • $50 ev Zvi †_‡K Kg n‡j ‡Kvb discount applied n‡e bv|
  • GLb Dc‡iv³ discount  logic implement Gi Rb¨ BasketDiscountMoneyOff bv‡g GKwU class Add Kwi Ges following code wjwL
namespace StrategyPattern.Model
{
public class BasketDiscountMoneyOff : IBasketDiscountStrategy
{
public decimal GetTotalCostAfterApplyingDiscountTo(Basket basket)
{
if (basket.TotalCost > 100)
return basket.TotalCost - 10m;
if (basket.TotalCost > 50)
return basket.TotalCost - 5m;
else
return basket.TotalCost;
}
}
}
  • Second discount strategy ‡Z basket’s total value Gi Dci discount percentage apply Kiv nq|
  • hLb GB discount apply nq customer total value Gi Dci 15% discount cv‡e|
  • IBasketDiscountStrategy interface ‡K implement K‡i Avgiv Av‡iKwU class Add Ki‡ev hvi name BasketDiscountPercentageOff Ges Dnvi code wb¤œiƒc-

namespace StrategyPattern.Model
{
public class BasketDiscountPercentageOff : IBasketDiscountStrategy
{
public decimal GetTotalCostAfterApplyingDiscountTo(Basket basket)
{
return basket.TotalCost * 0.85m;
}
}
}
  • Finally Avgiv special case discount strategy add Ki‡ev hv †Kvb discount bv _vK‡j use n‡e|
  • Bnv Null Object pattern implementat Ki‡e|
  • GRb¨ GKwU new class Add Ki‡ev hvnvi name n‡e NoBasketDiscount Ges wb¤§iƒc code definition hold Ki‡e-
namespace StrategyPattern.Model
{
public class NoBasketDiscount : IBasketDiscountStrategy
{
public decimal GetTotalCostAfterApplyingDiscountTo(Basket basket)
{
return basket.TotalCost;
}
}
}
  • GB discount strategy total basket cost return Ki‡e| G‡ÿ‡Î †Kvb discount algorithm apply n‡e bv|
  • GKwU basket G ‡Kvb strategy algorithm apply n‡e Zv determine Kivi Rb¨ Avgiv Factory Method pattern e¨envi Ki‡ev|
  • Factory Method pattern use K‡i correct discount strategy instance cvIqvi Rb¨ Avgiv GKwU enumeration define Ki‡ev Ges Dnvi information ‡K Dnvi argument wnmv‡e use Ki‡ev|
  • GRb¨ GLb Avgiv DiscountType bv‡g GKwU class define Ki‡ev hvi code wb¤œiƒc-
namespace StrategyPattern.Model
{
public enum DiscountType
{
NoDiscount = 0,
MoneyOff = 1,
PercentageOff = 2
}
}

  • GB enumeration ‡K Factory class Gi Factory method Gi argument wnmv‡e pass Ki‡ev Ges argument Abyhvqx exact discount strategy return Ki‡e|
  • GLb Factory method pattern create Kivi Rb¨ BasketDiscountFactory bv‡g GKwU class Add Ki‡ev hvnv GKwU static method contain Ki‡e| GB static method G Avgiv argument wnmv‡e enumeration pass Ki‡ev Ges D³ argument Gi Dci wfwË K‡i exact discount strategy return Ki‡e|
namespace StrategyPattern.Model
{
public class BasketDiscountFactory
{
public static IBasketDiscountStrategy GetDiscount(DiscountType DiscountType)
{
switch (DiscountType)
{
case DiscountType.MoneyOff:
return new BasketDiscountMoneyOff();
case DiscountType.PercentageOff:
return new BasketDiscountPercentageOff();
default:
return new NoBasketDiscount();
}
}
}
}
  • GLb Avgiv Basket class G KwZcq modification Ki‡ev
    • GKwU byZb constructor include Ki‡ev hvnv DiscountType enumeration ‡K argument wnmv‡e wb‡e|
    • Ges GKwU method include Ki‡ev hvnv  applied discount mnKv‡i basket  total cost return Ki‡e|
namespace StrategyPattern.Model
{
public class Basket
{
private IBasketDiscountStrategy _basketDiscount;
public Basket(DiscountType discountType)
{
_basketDiscount = BasketDiscountFactory.GetDiscount(discountType);
}
public decimal TotalCost { get; set; }
public decimal GetTotalCostAfterDiscount()
{
return _basketDiscount.GetTotalCostAfterApplyingDiscountTo(this);
}
}
}
  • Basket total price determine algorithm m¤ú‡K© m¤ú~b© ignore _v‡K| GRb¨ Bnv‡Z discount strategy inject Kiv nq| discount strategy inject Kivi Rb¨ Avgiv interface use Ki‡ev|
 









কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন