Thursday, February 15, 2007

 

Design Patterns

Nick Malik posted an interesting entry titled What are the top 100 patterns that every developer must know on his “Inside Architecture” blog. His question prompted a few replies including my own, which I’ve edited and reproduced here.

My own thoughts are in line with the comments made by JohnCJ. The common patterns in a particular domain maybe different from those in others, hence the drive behind creating Domain Specific Languages (DSLs). There might only be 10 - 20 truly common patterns, and even those may take different concrete forms depending on their context.

I would be surprised if an ‘average’ programmer (is there such a thing?) could name the ten most common patterns and adequately explain them to someone else, even though they may be familiar with the principles of good design. That is not a bad thing in itself, as developers encountering patterns for the first time are sometimes prone to over use them. Come to think of it can you name and accurately describe the 10 most common non-compound patterns? I struggled to remember these: Factory Method, Abstract Factory, Strategy, Observer, Decorator, Façade, Composite, Singleton, Adaptor, Command, Iterator, Proxy, State. I honestly don’t think I could remember 100 design patterns!

I think a better approach is to teach developers the Object Oriented design principles that underpin the creation of patterns and a few patterns not to use (anti-patterns).

The patterns themselves are not as important as the underlying principles:
  1. Encapsulate what varies.
  2. Program to an interface, rather than an implementation.
  3. Favor composition over inheritance.
  4. A Class should have only one reason to change (Single Responsibility Principle or SRP).
  5. Sub-types MUST be substitutable for their base (Liskov Substitution Principle or LSP).
  6. Classes should be open for extension, but closed for modification (Open Closed Principle or OCP).
The Portland pattern repository: http://c2.com/ppr/ is a good resource, although it can be a little hard to search. If you are interested in learning about Design Patterns you should read Head First Design Patterns by Elizabeth and Eric Freeman.


    

Powered by Blogger