现在的位置: 首页 > 综合 > 正文

开放性问题三(Decorator)

2011年12月04日 ⁄ 综合 ⁄ 共 819字 ⁄ 字号 评论关闭
Decorator
In the Implementation section of the Decorator Pattern, the authors write: A decorator object's interface must conform to the interface of the component it decorates.
Now consider an object A, that is decorated with an object B. Since object B "decorates" object A, object B shares an interface with object A. If some client is then passed an instance of this decorated object, and that method attempts to call a method in B that is not part of A's interface, does this mean that the object is no longer a Decorator, in the strict sense of the pattern? Furthermore, why is it important that a decorator object's interface conforms to the interface of the component. it decorates?

Decorator
在Decorator模式的实现章节,作者写到:一个decorator对象的接口必须服从它所装饰的部件的接口。
现在试想一个object A被object B装饰。既然object B“装饰”object A,object B与object A共享一个接口。如果向某个client传入这个已装饰对象(B)的实例,并且那个方法试图调用B中的一个方法,而该方法不在A的接口中,是否这就意味着从严格意义上讲该对象不再是一个“装饰者”?进一步说,为什么装饰者对象的接口服从被装饰部件的接口那么重要呢?

抱歉!评论已关闭.