Differences between Proxy and Decorator Pattern

前端 未结 8 638
自闭症患者
自闭症患者 2020-12-04 06:23

Can you give any good explanation what is the difference between Proxy and Decorator?

The main difference I see is that when we assume that Pro

8条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 06:30

    The real difference is not ownership (composition versus aggregation), but rather type-information.

    A Decorator is always passed its delegatee. A Proxy might create it himself, or he might have it injected.

    But a Proxy always knows the (more) specific type of the delegatee. In other words, the Proxy and its delegatee will have the same base type, but the Proxy points to some derived type. A Decorator points to its own base type. Thus, the difference is in compile-time information about the type of the delegatee.

    In a dynamic language, if the delegatee is injected and happens to have the same interface, then there is no difference.

    The answer to your question is "Yes".

提交回复
热议问题