装饰设计模式 [英] The Decorator Design Pattern

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

问题描述

我刚刚开始学习设计模式,而且我有两个与Decorator相关的问题...

I am just starting to learn design patterns and I have two questions related to the Decorator...

我想知道装饰器图案为什么建议装饰器实现它所装饰的组件的所有公共方法?

I was wondering why the decorator pattern suggests that the decorator implement all public methods of the component of which it decorates?

装饰器类不能用于提供额外的行为,然后是具体的组件另外,如果要装饰的具体组件没有一个基类,这个抽象装饰器可以用来调用其他的东西。

Can't the decorator class just be used to provide the additional behaviors, and then the concrete component (which is passed into it) just be used to call everything else?

还从派生出来?

提前感谢

推荐答案

你有误会的装饰。您正在考虑使用附加功能扩展具体类的简单情况。在这种情况下,在大多数OO语言中,派生类可以简单地允许其超类来处理任何未实现的方法。

I think you have have misunderstood Decorator. You're thinking of a simple case of extending a concrete class with additional functionality. In this case, yes in most OO languages the derived class can simply allow its superclass to handle any unimplemented methods.

class Base {

  function foo() {
    return "foo";
  }

  function bar() {
    return "bar";
  }

}

// Not what we think of as a Decorator,
// really just a subclass.
class Decorator extends Base {

  // foo() inherits behavior from parent Base class 

  function bar() {
    return parent::bar() . "!"; // add something
  }

}

装饰类不会扩展其装饰类的基类。它是一种不同的类型,它具有装饰类的成员对象。因此,它必须实现相同的接口,如果只是调用装饰对象的相应方法。

A Decorator class does not extend the base class of its "decorated" class. It is a different type, which has a member object of the decorated class. Thus it must implement the same interface, if only to call the respective method of the decorated object.

class Decorator { // extends nothing
  protected $base;

  function __construct(Base $base) {
    $this->base = $base;
  }

  function foo() {
    return $base->foo();
  }

  function bar() {
    return $base->foo() . "!"; // add something
  }

}

可能是值得为装饰类和Decorator类定义一个界面(如果你的语言支持这样的话)。这样你可以在编译时检查装饰器实现相同的界面。

It might be worthwhile to define an interface (if your language supports such a thing) for both the decorated class and the Decorator class. That way you can check at compile time that the Decorator implements the same interface.

interface IBase {
  function foo();
  function bar();
}

class Base implements IBase {
  . . .
}

class Decorator implements IBase {
  . . .
}






Re:@Yossi Dahan's评论:我在维基百科的文章中看到了歧义,但是如果仔细阅读,它会说装饰对象中正在装饰的组件是一个字段,并且组件作为参数传递给装饰器构造函数。这不同于继承。


Re: @Yossi Dahan's comment: I see the ambiguity in the wikipedia article, but if you read carefully it does say that the component being decorated is a field in the decorator object, and that the component is passed as an argument to the decorator constructor. This is different from inheritance.

尽管维基百科的文章说装饰器从组件继承,你应该把它看作是实现一个接口,就像我在PHP中展示一样上面的例子。装饰器仍然必须代替组件对象,如果它继承,它将不会。这允许装饰器装饰实现该接口的任何类的对象。

Though the wikipedia article does say the decorator inherits from the component, you should think of this as implementing an interface, as I showed in the PHP example above. The decorator still has to proxy for the component object, which it wouldn't if it had inherited. This allows the decorator to decorate an object of any class that implements that interface.

以下是设计模式:可重用元素的摘录Gamma,Helm,Johnson和Vlissides的面向对象软件:

Here are some excerpts from "Design Patterns: Elements of Reusable Object-Oriented Software" by Gamma, Helm, Johnson, and Vlissides:


装饰器



意图



动态附加对象
的附加责任。装饰器提供一个
灵活的替代方法来对扩展功能进行子类化

Decorator

Intent

Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

...
装饰器符合
的界面组件它装饰,以便
的存在对
组件的客户端是透明的。

... A decorator conforms to the interface of the component it decorates so that its presence is transparent to the component's clients.


  • 装饰器
    维护对Component对象的引用,并定义符合Component界面的

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

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