Head First Design Patterns - Building Extensible & Maintainable Object-Oriented Software

C++ implementation of example code from the book by Freeman et. al.

Source Code: https://bitbucket.org/williamblair/headfirstdesignpatternscpp/src/main/

Patterns Summary

StrategyEncapsulates interchangable behaviors and uses delegation to decide which one to use
ObserverAllows objects to be notified when the state changes
DecoratorWraps an object to provide new behavior
Factory MethodSubclasses decide which concrete classes to create
Abstract FactoryAllows a client to create families of objects without specifying their concrete classes
SingletonEnsures one and only one object is created
CommandEncapsulates a request as an object
AdapterWraps an object and provides a different interface to it
FacadeSimplifies the interface of a set of classes
Template MethodSubclasses decide how to implement stepns in an algorithm
IteratorProvides a way to traverse a collection of objects without exposing its implementation
CompositeClients treat collections of objects and individual objects uniformly
StateEncapsulates state-based behaviors and uses delegation to switch between behaviors
ProxyWraps an object to control access to it
Model-View-Controller (MVC)Compound pattern consisting of Observer, Strategy, Composite patterns.

Object-Oriented Principles Summary

  • Encapsulate what varies
  • Favor compisition over inheritance
  • Program to interfaces, not implementations
  • Strive for loosely coupled design between objects that interact
  • Open-closed principle: objects should be closed for modification and open for extension
  • Dependency-inversion principle: Depend upon abstractions, not concrete classes. High-level components should not depend on low-level components; they should both depend on abstractions.
  • Principle of least-knowlege: talk only to your immediate friends; only coupling should be between 2 immediate classes.
  • Hollywood principle: don't call us, we'll call you; high level components determine when lower level components are needed.
  • Single responsibility principle: a class should only have one reason to change.

Other Notes

  • Problems consists of goals and sets of constraints; known as "forces" to which patterns can be applied
  • Patterns can be categorized into creational, behavioral, and structural
  • KISS: Keep it simple - don't try to apply a design pattern unnecessarily if a simpler solution exists
  • If you don't need it now, don't do it now - avoid overengineering a solution if it isn't likely to change in the future
  • Anti-patterns: known BAD solutions that you should avoid - it looks good at first but turns out bad if applied

<-- Back to Home