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: http://github.com/johnlindquist/johnlindquist.com/tree/master/DecoratorPatternDemo/src/

15 comments:

  1. really cool, thanks

    ReplyDelete
  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

    ReplyDelete
  3. johnlindquist8:01 AM

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

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

    ReplyDelete
  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.

    ReplyDelete
  6. johnlindquist3:46 PM

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

    ReplyDelete
  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.

    ReplyDelete
  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!

    ReplyDelete
  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) :)

    ReplyDelete
  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.

    ReplyDelete
  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.

    ReplyDelete
  12. Awesome work. Keep em coming

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

    ReplyDelete
  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?

    ReplyDelete
  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.

    ReplyDelete
  15. Ignserra29:28 PM

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

    ReplyDelete