Anyone  have any good examples of the decorator pattern in the VCL or a #delphi framework somewhere?  I'd like to see it in the wild.

Comments

  1. Alternately, you can download the ModelMaker Design Patterns manual here: http://www.modelmakertools.com/modelmaker/download.html

    ReplyDelete
  2. Decorator pattern in the VCL?
    Start with TCompressionStream :)

    A

    ReplyDelete
  3. Bill -- Hey, thanks for the pointer to that MM document.  Very nice.

    ReplyDelete
  4. Nick Hodges My pleasure, Nick. What Gerrit shows for the Decorator is not so different to Yanniel's examples. There are also some articles out there which suggest combining the Decorator with the Fluent Interface. But I haven't decided yet how I feel about FI.

    ReplyDelete
  5. I use this a lot to add functionality to TStream. An example would be the TGpBufferedStream class from http://code.google.com/p/gpdelphiunits/source/browse/trunk/src/GpStreams.pas.

    On the other hand, aren't all 'helpers' decorators?

    ReplyDelete
  6. Primož Gabrijelčič Helpers were my first thought, but I thought they might take us down the FreeAndNil path....    ;)

    ReplyDelete
  7. I don't know if helpers are considered to be the decorator pattern or not, actually.

    Primož Gabrijelčič Thanks for the link -- I'll give it a look.

    ReplyDelete
  8. I've seen the word "decorator" mentioned in DUnit sources.

    ReplyDelete
  9. Nick Hodges Probably helpers are not really decorators, at least not as the examples we have seen. However, they do meet the requirements of the definition. The difference, as I see it, is that the examples show a decorator aggregating within itself the original class. I should add a disclaimer about wording, since I struggled with the description I presented, I think it conveys the sense, though it may not be entirely accurate.

    ReplyDelete
  10. Ondrej Kelle Good thought. DUnit may be one of the better sources to explore for patterns.

    ReplyDelete
  11. Maybe but it has so much decoration it is difficult to find the 'meat'.

    ReplyDelete
  12. Sorry, but i still quite not getting what decorator is,i've read the wiki about it, so is it the same as create a descendant class or is it more like helper class?

    ReplyDelete
  13. Iwan Cahyadi Sugeng  Decorator is just like a descendant class, the trick is that it takes an instance of base class in constructor, which allows you to decorate existing instances without needing to change the declaration of existing objects of base type

    ReplyDelete
  14. Nick Hodges Yes, his examples are pervasive. That one is a match to the one in my first link, which he published on Codeplex. Of the examples I have seen, his are the clearest to understand.

    ReplyDelete

Post a Comment