Decoration with several interface

前端 未结 3 1403
闹比i
闹比i 2021-02-09 20:19

I am always struggling with decoration + interfaces. Say I have the following \'behavior\' interfaces :

interface IFlyable { void Fly();}
interface ISwimmable {          


        
3条回答
  •  独厮守ぢ
    2021-02-09 20:55

    The Decorator Pattern is not intended for adding new methods to the decorated object. This is what you are trying to do, and it's impossible to do elegantly in a statically-typed language.

    What Decorator is useful for is where the interface to the decorators and the decorated component is the same, and the decorator adds a bit of extra functionality to the method either before or after passing the request along the decorator chain.

提交回复
热议问题