关于装饰图案和抽象装饰类的问题? [英] Question about decorator pattern and the abstract decorator class?

查看:117
本文介绍了关于装饰图案和抽象装饰类的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题已经此处,而不是回答具体问题,描述相反,装饰器图案的作用如何。我想再次问一次,因为通过阅读装饰图案的作品,我的答案并不是很明显的(我已经阅读了维基百科的文章和头部设计模式一节)。

This question was asked already here, but rather than answering the specific question, descriptions of how the decorator pattern works were given instead. I'd like to ask it again because the answer is not immediately evident to me just by reading how the decorator pattern works (I've read the wikipedia article and the section in the book Head First Design Patterns).

基本上,我想知道为什么必须创建一个实现(或扩展)一些接口(或抽象类)的抽象装饰类。所有新的装饰类不能简单地实现(或扩展)基本抽象对象本身(而不是扩展抽象装饰器类)?

Basically, I want to know why an abstract decorator class must be created which implements (or extends) some interface (or abstract class). Why can't all the new "decorated classes" simply implement (or extend) the base abstract object themselves (instead of extending the abstract decorator class)?

更具体的,我将使用与咖啡饮料相关的设计模式书中的示例:

To make this more concrete I'll use the example from the design patterns book dealing with coffee beverages:


  • 有一个名为饮料

  • 简单的饮料类型,如 HouseBlend 只需扩展饮料

  • 为了装饰饮料,创建了一个抽象的 CondimentDecorator 类,它扩展了饮料并且具有饮料

  • 的实例说我们要添加一个牛奶调味品,一个类牛奶被创建,扩展 CondimentDecorator

  • There is an abstract component class called Beverage
  • Simple beverage types such as HouseBlend simply extend Beverage
  • To decorate beverage, an abstract CondimentDecorator class is created which extends Beverage and has an instance of Beverage
  • Say we want to add a "milk" condiment, a class Milk is created which extends CondimentDecorator

我想了解为什么我们需要 CondimentDecorator 类,为什么类 Milk couldn'只需简单地扩展 Beverag e 类本身,并在其构造函数中传递了一个 Beverage 的实例。

I'd like to understand why we needed the CondimentDecorator class and why the class Milk couldn't have simply extended the Beverage class itself and been passed an instance of Beverage in its constructor.

希望这很清楚... 如果不是我只想知道为什么这个模式需要抽象装饰类??谢谢。

Hopefully this is clear...if not I'd simply like to know why is the abstract decorator class necessary for this pattern? Thanks.

编辑:我试图实现这一点,省略抽象装饰类,并且它似乎仍然可以工作。这个抽象类是否存在于这个模式的所有描述中,只是因为它为所有新的装饰类提供了一个标准接口。

I tried to implement this, omitting the abstract decorator class, and it seems to still work. Is this abstract class present in all descriptions of this pattern simply because it provides a standard interface for all of the new decorated classes?

推荐答案

它可以独立地使用各种装饰器以不同的组合装饰基础,而不必为每个可能的组合导出类。例如,假设您想要使用牛奶和肉豆蔻您的饮料。使用基于抽象装饰器类的装饰器,您只需使用 Milk Nutmeg 装饰器包装。如果它是从饮料派生的,那么你必须要有一个 MilkWithNutmegBeverage 类和一个 MilkBeverage 类和一个 NutmegBeverage 类。你可以想象,随着可能的组合数量的增加,这种爆炸是如何爆炸的。使用抽象装饰器实现将这减少到每个装饰只需一个装饰器类实现。

It enables the decoration of the base class independently with various decorators in different combinations without having to derive a class for each possible combination. For example, say you want your Beverage with milk and nutmeg. Using decorators based on the abstract decorator class, you merely wrap with with Milk and Nutmeg decorators. If it was derived from Beverage, you'd have to have a MilkWithNutmegBeverage class and a MilkBeverage class and a NutmegBeverage class. You can imagine how this explodes as the number of possible combinations increases. Using the abstract decorator implementation reduces this to just one decorator class implementation per decoration.

这篇关于关于装饰图案和抽象装饰类的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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