সোমবার, ১৮ নভেম্বর, ২০১৩

Template Method Pattern



  • Gang of Four Gi behavioral pattern
  • hLb †Kvb GKwU algorithm Gi skeleton define Kiv nq hvnvi KwZcq steps subclasses ‡_‡K deferred nq ZLb Template Method e¨envi K‡i  subclass algorithm Gi step mg~n redefine Kiv nq (algorithm’s structure cwieZ©b bv K‡i) i.e. Base Class Gi algorithm skeleton Gi KwZcq steps subclasses ‡_‡K deferred nq Ges Template Method e¨envi K‡i subclass algorithm Gi step mg~n algorithm’s structure cwieZ©b bv K‡i redefine Kiv nq|

Intent:
  • Base Class Gi algorithm skeleton Gi KwZcq steps subclasses ‡_‡K deferred nq Ges Template Method e¨envi K‡i subclass algorithm Gi step mg~n algorithm’s structure cwieZ©b bv K‡i redefine Kiv nq|
UML Diagram:
  • wb‡¤œ Template pattern Gi UML representation Kiv n‡jvt
 
 

  • Dc‡ii diagramwU Template pattern define K‡i|
  • Dc‡ii diagramwUi class mg~‡ni role wb¤œiƒct-
    • AbstractClass abstract steps Gi mv‡_ AbstractClass skeleton process workflow define K‡i| Bnv ConcreteClassA Ges ConcreteClassB ‡K override I implement K‡i|
    • Bnv structure  wVK †i‡L subclass Gi Dci  depend K‡I algorithm details altering enable K‡i |
    • ConcreteClassA Ges ConcreteClassB AbstractClass ‡_‡K inherit nq, abstract method mg~n implement K‡i Ges algorithm detail hold K‡i|
  Real Life Example:

·         g‡bKwi Home builder company Zv‡`i GKwU building Gi A‡bK¸‡jv subdivision Ki‡e|
·         Subdivision Gi  ‡ÿ‡Î floor plan n‡jv skeleton. Bnv fixed _vK‡e  i.e. cÖwZwU subdivision Gi mgmsL¨vK floor _vK‡e wKš‘ cÖwZwU subdivision Gi floor construction i.e. floor decoration different n‡e| D`vnib ¯^i~c †Kvb subdivision G nq‡Zv Fireplace _vK‡e..
·         Template Method  Gi †ÿ‡ÎI cÖ_‡g GKwU algorithm Gi skeleton define Kiv nq Ges †mB Abyhvqx subclass mg~n define Kiv nq Ges †ÿÎwe‡k‡l subclass mg~‡ni KwZcq step defer K‡i|


Code Example
  • GB example G GKwU e-commerce site Gi order return system Gi Dci Template pattern apply Ki‡ev|
  • cÖwZwU order return G returning process Gi Dci wfwË K‡i wewfbœ ai‡bi series of process m¤úbœ nq|
  • GLv‡b `ywU Kvi‡b order return nq-
    • No quibbles return
    • Faulty order return
  • No  quibbles return G goods Gi price ‡_‡K original post and packaging price deduct(minus) Kiv nq Ges finally product mg~n stock G return nq|
  • Faulty return ZLbB nq hLb customer faulty item receive K‡i Ges refund Ki‡Z Pvq| GB‡ÿ‡Î original post and packaging payment Gi mv‡_
  • wb‡¤œi wP‡Î exercise Gi mv‡_ involved class mg~n †`Iqv n‡jvt-


  • cÖ_‡g GKwU solution create Kwi Ges Dnvi bvg w`B TemplateMethodPattern.
  • Then solution G GKwU C# class library project add Kwi Ges Dnvi name w`B TemplateMethodPattern.Model
  • Gevi Dnv‡Z GKwU new class add Kwi Ges Dnvi name w`B ReturnAction Ges Dnvi code listing wb¤œiƒc n‡e-
namespace TemplateMethodPattern.Model
{
public enum ReturnAction
{
FaultyReturn = 0,
NoQuibblesReturn = 1
}
}
  • GB enumeration Gi gva¨‡g return order Gi type detect Kiv n‡e|
  • Then return order entity Gi Rb¨ ReturnOrder.cs bv‡g GKwU class define Ki‡ev hvi definition wb¤œiƒct
namespace TemplateMethodPattern.Model
{
public class ReturnOrder
{
public ReturnAction Action { get; set; }
public string PaymentTransactionId { get; set; }
public decimal PricePaid { get; set; }
public decimal PostageCost { get; set; }
public long ProductId { get; set; }
public decimal AmountToRefund { get; set; }
}
}
  • ReturnOrder entity Øviv customer’s order returned n‡q‡Q wK bv Zv represent Kiv nq|
  • Action property Øviv return order type determine Kiv nq|
  • PaymentTransactionId Øviv order purchase Gi Rb¨ e¨eüZ original payment refer Kiv nq|
  • PricePaid  Ges PostageCost Øviv order total Ges shipping costs refer K‡i|
  • ProductId Øviv returned product Gi unique identifier hold K‡i|
  • AmountToRefund Øviv customer ‡K refunded amount refer K‡i|
  • Domain model Øviv abstract template method ‡K specific faulty Ges no quibbles subclass G implement I override Kiv nq|
  • Template method create Kivi Rb¨ project G GKwU new class add Kwi Ges Dnvi bvg w`B ReturnProcessTemplate. Bnv GKwU abstract class Ges Bnvi definition wb¤œiƒc n‡e-
namespace TemplateMethodPattern.Model
{
public abstract class ReturnProcessTemplate
{
protected abstract void GenerateReturnTransactionFor(ReturnOrder returnOrder);
protected abstract void CalculateRefundFor(ReturnOrder returnOrder);

public void Process(ReturnOrder returnOrder)
{
GenerateReturnTransactionFor(returnOrder);
CalculateRefundFor(returnOrder);
}
}
}
  • Dc‡iv³ code definition অনুযায়ী class এবং প্রথম method দুইটি abstract. সুতরাং ইহাদেরকে subclass implement করতে হবে.
  • Third method ১ ও ২ নং abstract method call K‡i Ges ReturnOrder entity ‡K argument wnmv‡e pass K‡i|
  • GLb Avgiv template method Øq‡K subclass G add Ki‡ev|
  • GRb¨ cÖ_‡g Avgiv project G GKwU new class add Ki‡ev Ges Dnvi name w`‡ev NoQuibblesReturnProcess Ges Dnvi code listing wb¤œiƒc n‡e-

namespace TemplateMethodPattern.Model
{
public class NoQuibblesReturnProcess : ReturnProcessTemplate
{
protected override void GenerateReturnTransactionFor(ReturnOrder returnOrder)
{
// Code to put items back into stock...
}
protected override void CalculateRefundFor(ReturnOrder returnOrder)
{
ReturnOrder.AmountToRefund = returnOrder.PricePaid;
}
}
}
  • NoQuibblesReturnProcess stock G item mg~n return K‡i| GB logic GenerateReturnTransactionFor method Gi override method G Ae¯_vb K‡i|
  • CalculateRefundFor overridden method return order Gi  product  Gi price  G AmountToRefund property set K‡i|
  • G‡ÿ‡Î †Kvb postage cost refund nq bv|
  • Second subclass I  ReturnProcessTemplate  ‡K inherit K‡i| GB subclass wU Faulty Return Process Gi Rb¨ KvR K‡i| GRb¨ GB subclass  wUi  name n‡e   FaultyReturnProcess class
  • g~jZt GB class wU faulty returned item mg~n processing Gi Rb¨ e¨eüZ n‡e|
  • FaultyReturnProcess class Gi code wb¤œiƒc n‡e-
namespace TemplateMethodPattern.Model
{
public class FaultyReturnProcess : ReturnProcessTemplate
{
protected override void GenerateReturnTransactionFor(ReturnOrder returnOrder)
{
// Code to send generate order that sends faulty item back to
// manufacturer...
}
protected override void CalculateRefundFor(ReturnOrder returnOrder)
{
ReturnOrder.AmountToRefund = returnOrder.PricePaid +
returnOrder.PostageCost;
}
}
}
  • Overridden method i.e.  GenerateReturnTransactionFor method faulty item mg~n refund Kivi Rb¨ e¨eüZ nq|
  • CalculateRefundFor method wU NoQuibblesReturnProcess class Gi CalculateRefundFor method Gi logic ‡_‡K differ K‡i i.e. G‡ÿ‡Î c~‡e©v³ class Gi i.e. NoQuibblesReturnProcess Gi CalculateRefundFor method Gi logic Ges eZ©gvb class Gi CalculateRefundFor method Gi logic wfbœZi n‡e|
  • Item returning process coordinate Gi Rb¨ Service class create Kivi c~‡e© order  type  Gi Dci base K‡i correct returning process obtain Kivi Rb¨ Avgiv Factory method pattern use Ki‡ev|
  • GKwU new class Create Kwi Ges Dnvi name w`B ReturnProcessFactory. G&B class Gi gva¨‡gB Avgiv correct processing object obtain Ki‡Z cvi‡ev| wb‡¤œ GB class Gi details ‡`qv n‡jv-

namespace TemplateMethodPattern.Model
{
public static class ReturnProcessFactory
{
public static ReturnProcessTemplate CreateFrom(ReturnAction returnAction)
{
switch (returnAction)
{
case (ReturnAction.FaultyReturn):
return new FaultyReturnProcess();
case (ReturnAction.NoQuibblesReturn):
return new NoQuibblesReturnProcess();
default:
throw new ApplicationException(
“No Process Template defined for Return Action of “ + returnAction.ToString());
}
}
}
}
  • Factory method pattern e¨envi K‡i client end G object creation complexity hide Kiv nq Ges logic mg~n‡K centralize Kiv nq|
  • GLb ReturnOrderService class add Ki‡ev  i.e. Item returning process coordinate Kivi Rb¨ Service class add Ki‡ev hvi code wb¤œiƒct
namespace TemplateMethodPattern.Model
{
public class ReturnService
{
public void Process(ReturnOrder returnOrder)
{
ReturnProcessTemplate returnProcess =
ReturnProcessFactory.CreateFrom(returnOrder.Action);
returnProcess.Process(returnOrder);
// Code to refund the money back to the customer...
}
}
}
  • Service Gi GKwU simple Process method _v‡K hvnv  ReturnOrder ‡K argument wnmv‡e †bq|
  • Service cÖ_‡g factory use K‡i correct ReturnProcessTemplate implementation G obtain K‡i then ReturnOrder entity pass K‡i Ges ReturnProcessTemplate G Process method call K‡i|
  • Subclass’s  method use K‡i call item mg~n return K‡i Ges customer receivable amount calculate K‡i|
  • hLb series of subclasses Gi code common nq ZLb Template Method pattern use K‡i code centralize Kiv nq|






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

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