Monday, December 20, 2010

PatternCraft – Decorator Pattern

PatternCraft is a series of video tutorials that use StarCraft references to teach Design Patterns.

This video is the sixth in the series and covers the Decorator Pattern.

Source is here:


  1. really cool, thanks

  2. hey tkx for this but...
    now how do you access the real marine object when you set a decorator to it ?
    as marine is now an ArmorUpgrade... I feel this is complicating everything

  3. johnlindquist8:01 AM

    The Marine is an "Infantry", so you access him as an Infantry.

  4. I missed Pattercrafts so much. Thanks for the next one.

  5. yeah, but in your case, you now need to access the real marine object from the marine.infantry var.
    I think it's an unneeded call and even if you expect to access it that way all the time, it looks a bit weird to me:
    to start with a marine var that references a Marine object whose properties can be accessed normally to a marine var that now references a Decorator object... and now you need to access the infantry var from this object to access the Marine object that we had in the beginning... this looks really weird to me.
    I think this is complicating the whole process... at least in this case.

  6. johnlindquist3:46 PM

    What infantry var? There's no marine.infantry property...

  7. johnlindquist3:48 PM

    Honestly, I agree the Decorator pattern can be over-complicated (especially for such a simple example). But you're trading that bit of complexity for being able to create a wide array of concrete base classes a decorator classes that all fit together beautifully.

  8. Jackson Dunstan4:14 PM

    Very nice video, as usual. I was quite thrown by the ArmorUpgrade being an Infinity for a while, but I think that's a problem that can be fixed with some better naming. Nice demonstration!

  9. johnlindquist4:19 PM

    Yeah, ArmorUpgrade is an Infantry only to be able to match the type with the Concrete classes. It's fooling inheritance into thinking that it's allowed to override (through delegation) :)

  10. in your example, your marine var becomes an InfantryDecorator object and to access the initial Marine object we need to call the infantry property of the newly created InfantryDecorator object...
    But this pattern can give ideas of how to implement something a bit simplier, at least for this example.

  11. johnlindquist4:24 PM

    Unfortuantely, this is about as simple as I can make it. The concepts of Wrapping/Delegation can only be explained through the architecture (that's why I kept jumping back to the UML diagram) and the benefits only show when working on larger projects with many classes.

  12. Awesome work. Keep em coming

    How do you remove an upgrade though ? Create the opposite decorator ?

  13. Equ___12:34 PM

    UML diagrams generation for as3 classes is built-in by default or you need to install some plugins to get them?

  14. johnlindquist9:35 PM

    UML diagrams are included as a plugin of IntelliJ. You just need to make sure the plugin is checked when you install it.

  15. Ignserra29:28 PM

    Thanks for all your effort, all your tutorials help me to understand design patters better! keep em coming!