Differences between Proxy and Decorator Pattern

前端 未结 8 639
自闭症患者
自闭症患者 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

    Key differences:

    1. Proxy provides the same interface. Decorator provides an enhanced interface.
    2. Decorator and Proxy have different purposes but similar structures. Both describe how to provide a level of indirection to another object, and the implementations keep a reference to the object to which they forward requests.
    3. Decorator can be viewed as a degenerate Composite with only one component. However, a Decorator adds additional responsibilities - it isn't intended for object aggregation.
    4. Decorator supports recursive composition
    5. The Decorator class declares a composition relationship to the LCD (Lowest Class Denominator) interface, and this data member is initialized in its constructor.
    6. Use Proxy for lazy initialization, performance improvement by caching the object and controlling access to the client/caller

    Sourcemaking article quotes the similarities and differences in excellent way.

    Related SE questions/links:

    When to Use the Decorator Pattern?

    What is the exact difference between Adapter and Proxy patterns?

提交回复
热议问题