Tuesday, November 29, 2011

10 Design Principles

Was going through my older notes, brushing up some technical bits. My first book for design patterns was the original book by the Gang of Four. However, the book that helped me understand Design Patterns the best was Head First Design Patterns. Following are the key design principles summarised in the book:
  1. Identify the aspects of the application that vary and separate them from what stays the same: use encapsulation.
  2. Program to an interface, not an implementation
  3. Favour composition over inheritance
  4. Strive for loosely coupled designs between objects that interact
  5. While inheritance is powerful, it does not always lead to the most flexible or maintainable designs
  6. Code should be closed to change but open to extension
  7. Principle of least knowledge: talk only to your immediate friends. Only invoke methods that belong to:
    • The object itself
    • Objects passed in a parameter to the method
    • Any object that the method creates or instantiates
    • Any components of the object
  8. Depend upon abstractions. Do not depend upon concrete classes
    • Avoid variables that hold reference to a concrete class (use Factory)
    • Avoid deriving class from concrete classes
    • Avoid overriding any of the implemented methods of a base class
    • You can ignore these guidelines if the class is highly unlikely to change
  9. Hollywood principle: don’t call us, we’ll call you. Low level components can hook into the system, but high level components determine when they are needed.
  10. A class should have only one reason to change
I have put this blog for my own reference, but hope it helps somebody else as well. To learn more, you will have to refer Head First. In next few posts I will summarise key design patterns as described in the book.