Xiao Peng

My personal blog moves to xiaopeng.me , blogs about design patterns will be synced to here.
肖鹏,ThoughtWorks资深咨询师,目前关注于架构模式、敏捷软件开发等领域,并致力于软件开发最佳实践的推广和应用。
多次为国内大型企业敏捷组织转型提供咨询和培训服务,在大型团队持续集成方面具有丰富的经验。
  博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

开放性问题三(Decorator)

Posted on 2006-08-02 09:36  勇敢的鸵鸟  阅读(418)  评论(0编辑  收藏  举报
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的接口中,是否这就意味着从严格意义上讲该对象不再是一个“装饰者”?进一步说,为什么装饰者对象的接口服从被装饰部件的接口那么重要呢?