The Decorator Pattern & Extension Methods in c#

前端 未结 5 1983
情歌与酒
情歌与酒 2021-02-04 01:59

Before going to describe my problem first,I would like to define definitions of Decorator and Extension method Decorator

Attach additiona

5条回答
  •  隐瞒了意图╮
    2021-02-04 02:24

    This explanation by Erich Gamma (GoF) seems to be the best... http://www.mif.vu.lt/~plukas/resources/Extension%20Objects/ExtensionObjectsPattern%20Gamma96.pdf

    Essentially stating that

    a) Combining all the operations and state that the different clients (present and future) need into a single interface results in a bloated interface

    b) Desired operations (known and unknown) can be categorized into components. From which one (or more) Component Interfaces) (extended interfaces) can be defined. These may or may not be implemented by objects (current and future).

    c) Clients that wish to use this extended interface can query whether a component supports it

    d) Finally, this extended interface has a common base class (ComponentExtension) with minimal interface to manage the extension itself (checking if an extension exists, informing the extension that it is about to be deleted)

    To be used when:

    1 When your existing classes may need additional and unforeseen interfaces (i.e. new, currently unknown patterns of behavior).

    2 When a class representing a key abstraction plays different (unforeseen & open-ended) roles for different clients.

    3 You wish to extend without sub-classing

    It is similar to the following patterns

    Visitor which needs stable class hierarchy & introduces dependency cycle

    Decorator where the use is more transparent, the interface is narrow and existing operations should be augmented

    Adapter which supports EXISTING interfaces

提交回复
热议问题