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:
Depend upon abstractions. Do not depend upon concrete classes
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.
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.
- Identify the aspects of the application that vary and separate them from what stays the same: use encapsulation.
- Program to an interface, not an implementation
- Favour composition over inheritance
- Strive for loosely coupled designs between objects that interact
- While inheritance is powerful, it does not always lead to the most flexible or maintainable designs
- Code should be closed to change but open to extension
- 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
- 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