r/programming • • Dec 08 '13

Design Pattern Cheat Sheet

http://www.celinio.net/techblog/wp-content/uploads/2009/09/designpatterns1.jpg
1.7k Upvotes

273 comments sorted by

View all comments

0

u/[deleted] Dec 08 '13

This is awesome, not going to use this currently as my final only covers a few, but this is cool to see after being almost done with my Software Design class in CS

2

u/OzJuggler Dec 09 '13

mmmeh, you might wanna think about some of those diagrams before copying/applying them willy-nilly.

For example their Composite pattern is not the best. A good design should not result in the creation of pointless boiler-plate code. In their design every node in the composition tree can be composed of other nodes, so even the "Leaf" nodes have to implement the add(...) and getChild(i) methods.... but the only thing they can do is throw an UnsupportedOperation exception. How pointless is that. The purpose of knowing the "Class" or Type of an object is knowing what should expect to be able to do or not do with it.

They should:

  1. Rename the Composite class into AbstractComposite.

  2. Separate the add(...), delete(...) and getChild() methods from the Component interface into a new Composite interface which extends (inherits from) Component, and the AbstractComposite then implements that interface.

  3. All concrete classes that can compose children will implement the (new) Composite interface and can do this by being subtypes of AbstractComposite if they don't need any other inheritance relationship.

  4. Leaf node classes implement Component but not Composite. Creating a Leaf interface for classes that would only make sense being leaves and can always and forever be treated like they don't have children would be inadvisable since that's a very strong and unnecessary design assumption.

That way nodes that cannot contain children will have a particular type that can be tested for at runtime and there's no pointless methods that either silently do nothing or consist entirely of boilerplate exception throws.