Differences between Proxy and Decorator Pattern

前端 未结 8 651
自闭症患者
自闭症患者 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条回答
  •  猫巷女王i
    2020-12-04 06:26

    Decorator get reference for decorated object (usually through constructor) while Proxy responsible to do that by himself.

    Proxy may not instantiate wrapping object at all (like this do ORMs to prevent unnecessary access to DB if object fields/getters are not used) while Decorator always hold link to actual wrapped instance.

    Proxy usually used by frameworks to add security or caching/lazing and constructed by framework (not by regular developer itself).

    Decorator usually used to add new behavior to old or legacy classes by developer itself based on interface rather then actual class (so it work on wide range of interface instances, Proxy is around concrete class).

提交回复
热议问题