- Decorator pattern structural patterns group Gi AšÍf©~³ GKwU pattern
- GB pattern use K‡i †Kvb existing object Gi behavior alter bv K‡i on the fly new behavior add Kiv hvq|
intent
- Decorator pattern composition Gi gva¨‡g dynamically object G new behavior add K‡i|
- Either same base class ‡_‡K inheriting Gi gva¨‡g wKsev shared interface implementing I class instance injecting Gi gva¨‡g decorated Kiv nq i.e. interface ‡K class constructor Øviv injecting Gi gva¨‡g Decorator pattern achieve Kiv hvq|
- Ab¨fv‡e ejv hvq Decorator pattern n‡jv Ggb GK ai‡bi process hvi gva¨‡g existing ‡Kvb class ‡K behavior or state extend K‡i Ggb class Øviv wrapping Kiv nq|
- Extended behavior combine Kivi Rb¨ Multiple decorators add Kiv hvq|
UMl
- wb‡¤œi Figure Decorator pattern Gi UML representation show K‡i|
- Zv‡`i role mg~n wb¤œiƒct-
ü IProduct product
Gi Rb¨ defined GKwU interface
ü DefaultProduct Ges ProductDecorator Aek¨B IProduct interface ‡K implement
Ki‡e|
ü DefaultProduct class Gi base functionality provide Ki‡e|
ü ProductDecorator IProduct interface ‡K implement
K‡i Ges
IProduct instance
Gi GKwU
reference wb‡q injected nq| d‡j inner
instance wrapped enable nq|
ü ConcreteDecoratorA I ConcreteDecoratorA ProductDecorator ‡_‡K inherit nq Ges IProduct instance G state Ges new behavior add K‡i|
Real Life Example
- Painting GKwU component
- Frame GKwU component
- Matt GKwU component
- Hanging Gi c~‡e© painting ‡K matted I framed Kiv nq|
- hw`I Painting, Frame, Matt wZbwU Avjv`v Avjv`v component Z_vwc Bnv hLb †`Iqv‡j Uvbv‡bv nq ZLb Bnv single visual component wnmv‡e `k©bv_©x‡`i `„wó‡MvPi nq|
- Abyiƒcfv‡e Decorator Pattern Gi †ÿ‡ÎI GKvwaK class Gi combination G GKwU class ‰Zix nq i.e. GKwU class Av‡iKwU‡K decorate K‡i wKš‘ hLb User Dnv use K‡i ZLb single class Gi b¨vq behave K‡i|
Steps For
Implementation
Step 1: Create an interface.
public interface IPrice
{
decimal Cost(decimal
quantity, decimal rate);
}
Step 2: Create concrete classes implementing the same interface.
public class Price:IPrice
{
public decimal
Cost(decimal quantity, decimal
rate)
{
return
quantity * rate;
}
}
Step 3: Create decorator classes implementing the interface as well as
injecting the interface through constructor.
public class PriceWithDiscountAndVat:IPrice
{
protected
decimal _vat;
protected
decimal _discount;
protected
IPrice _price;
public PriceWithDiscountAndVat(IPrice price, decimal
vat, decimal discount)
{
this._price
= price;
this._discount
= discount;
this._vat
= vat;
}
public decimal Cost(decimal
quantity, decimal rate)
{
return
(_price.Cost(quantity, rate) + _vat) - _discount;
}
}
public class PriceWithoutDiscount:IPrice
{
protected
decimal _vat;
protected
IPrice _price;
public
PriceWithoutDiscount(IPrice price, decimal vat)
{
this._price
= price;
this._vat
= vat;
}
public decimal Cost(decimal
quantity, decimal rate)
{
return
_price.Cost(quantity, rate) + _vat;
}
}
Step 4: Call decorated classes for getting custom behavior
class Program
{
static void Main(string[]
args)
{
decimal
quantity = 100;
decimal
rate = 20.44m;
decimal
vat = quantity*rate*0.1m;
decimal
discount = quantity*rate*0.15m;
IPrice
price = new PriceWithDiscountAndVat(new Price(),
vat,quantity);
IPrice
priceWithoutDiscount = new PriceWithoutDiscount(new Price(), vat);
Console.Write("Price With Discount = " +
price.Cost(quantity, rate).ToString());
Console.Write("\n");
Console.Write("Price Without Discount = " +
priceWithoutDiscount.Cost(quantity, rate).ToString());
Console.ReadKey();
}
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন