在装饰模式中添加状态 [英] Adding State in Decorator Pattern

查看:141
本文介绍了在装饰模式中添加状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将状态添加到消费者可用的装饰器链中。给定这个简化的模型:

 抽象类AbstractPizza {
public abstract print(...);
}

class Pizza:AbstractPizza {
public int Size(get; set;)
public print(...);
}

抽象类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 =蓝色;

HotBigCheesyPizza.print();
HotBigCheesyPizza.size = 28; // ERRRRRR !!!!!!!!!!!!!!!!!!!!!!!
}

现在,如果他们都实现了打印方法,并传播尽管链,它是都好。但是,这对国家是如何工作的呢?我无法访问HotBigCheesyPizza上的大小属性。



我缺少什么部分?错误的模式?



感谢您的帮助!
Cheers

解决方案

装饰器模式是为了在没有客户端需要调整的情况下为装饰类增加额外的行为。因此,它不是为了在装饰的东西上添加一个新的界面(例如 hotness cheese )。 p>

一个有些糟糕的例子,它可能用于什么地方你想改变 size 是如何计算的:你可以创建一个 c> MetricSizePizzaDecorator ,将尺寸转换为英制/公制单位。客户端不会知道比萨已经被装饰 - 它只是调用 getSize(),并且对结果进行任何处理(例如计算价格)。



我可能不会在我的例子中使用装饰器,但关键是:它不会改变界面。事实上,几乎所有的设计模式都归结为这样 - 在不改变界面的情况下增加设计的变化性。


I wonder how to add state to the chain of decorators that will be available to the consumer. Given this simplified model:

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 !!!!!!!!!!!!!!!!!!!!!!!
}

Now if they all implement the print method and propagate that though the chain, it's all good. But how does that work for the state? I can't access the size property on the HotBigCheesyPizza.

What's the part that I'm missing? Wrong pattern?

Thanks for helping! Cheers

解决方案

The decorator pattern is for adding additional behavior to the decorated class without the client needing to adjust. Thus it is not intended for adding a new interface (e.g. hotness, cheese) to the thing being decorated.

A somewhat bad example of what it might be used for is where you want to change how size is calculated: you could create a MetricSizePizzaDecorator that converts the size to/from English/metric units. The client would not know the pizza has been decorated - it just calls getSize() and does whatever it needs to do with the result (for example, to calculate the price).

I would probably not use the decorator in my example, but the point is: it does not alter the interface. In fact, nearly all design patterns come down to that - adding variability to a design without changing interfaces.

这篇关于在装饰模式中添加状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆