我想知道如何將狀態添加到可供用戶使用的裝飾器鏈中。鑑於這個簡化的模型:在修飾器模式中添加狀態
abstract class AbstractPizza{
public abstract print(...);
}
class Pizza:AbstractPizza{
public int Size (get; set;)
public print(...);
}
abstract class AbstractPizzaDecorator{
public Pizza:AbstractPizza;
public abstract print();
}
class HotPizzaDecorator:AbstractPizzaDecorator{
public int Hotness (get; set;)
public print(...);
}
class CheesyPizzaDecorator:AbstractPizzaDecorator{
public string Cheese (get; set;)
public print(...);
}
void Main()
{
BigPizza = new Pizza();
BigPizza.Size = 36;
HotBigPizza = new HotPizzaDecorator();
HotBigPizza.Pizza = BigPizza;
HotBigPizza.Hotness = 3;
HotBigCheesyPizza = new CheesyPizzaDecorator();
HotBigCheesyPizza.Pizza = HotBigPizza;
HotBigCheesyPizza.Cheese = "Blue";
HotBigCheesyPizza.print();
HotBigCheesyPizza.size = 28; //ERRRRRR !!!!!!!!!!!!!!!!!!!!!!!
}
現在,如果他們都實現了打印方法,並通過鏈傳播,這一切都很好。但是這對國家有什麼用?我無法訪問HotBigCheesyPizza上的size屬性。
我失蹤的部分是什麼?錯誤的模式?
感謝您的幫助! Cheers
你將如何建立HotBigCheesyPizza如果您HotPizzaDecorator不接受CheesyPizzaDecorator改寫......你能不能給個例子? –