Why do the references in a decorator and in a prox

2019-07-27 02:58发布

问题:

From Design Pattern by Gang of Four

Why does the reference component of the decorator Decorator to the decorated point to the interface Component of the decorated,

while the reference realSubject of the proxy Proxy point to the concrete RealSubject?

Thanks.

回答1:

Proxy may point exclusively to its subject's interface. The GoF mentions,

Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same.

But depending on implementation, a Proxy may instantiate its subject by calling a constructor, which necessitates referencing a concrete object.