Monday, March 25, 2013

Design Pattern Reference

Here are some of the best articles on design patterns on the internet. Once you review the examples and discussions, its best to do some comparisons next.

  1. Facade.
  2. Flyweight
  3. Adapter
  4. Chain of Responsibility
  5. Singleton
  6. Observer
  7. Composite
  8. Command
  9. Strategy
  10. Proxy.
  11. Decorator
  12. Broker
  13. Builder
  14. Dependency Injection
  15. State
  16. Bridge
  17. Interface
  18. Prototype
Interesting comparisons to do:

Facade, Adapter: Facade works on an entire subsystem (multiple classes) to make suability simpler and Adapter generally does not target simplicity as much as mapping one set of calls to another.
Proxy, Decorator: Proxy pattern binds the class being proxied with the proxy class at compile time. Decorator does this at runtime. Decorator typically intends to add or remove functionality while Proxy typically exposes full functionality. In most cases, proxy always uses lazy instantiation for the class which is being proxied and decorator will typically instantiate the containing class in the constructor.
Chain of Responsibility, Decorator: Think linked lists v/s wrappers. Linked lists can kill the flow at any time w/o visiting all elements. In addition wrappers implement both pre and post processors.
Decorator, Inheritance: If each operation implemented by a decorator is instead implemented as a sub class, you will have to create multiple combination sub-classes for each combination which can be produced using a decorator sequence. Think m+n v/s m 'times' n for the total number of classes.
Strategy, Dependency Injection: DI is a refinement of Strategy pattern.
Strategy, Command: Strategy pattern can be used when implementations change for a task amongst different classes (which implement the same interface). Command on the other hand is much simpler. It makes sense when the caller does not want to get involved in the details of how a handler implements a certain command.

No comments: