• Marie Antoinette •“Shape clay into a vessel, it is the space within that gives it value. Place doors and windows in a house, it is the opening that brings light within. Set spokes within a wheel, it is the emptiness of the hub that makes them useful.” • Tao Te Ching
Impossible to get right first time • Design patterns are • More flexible than software tends to be written first time • Contain distilled experience • Learnable • Common vocabulary • Design patterns are not • Difficult to understand • Unusual/sorcery • Algorithms • Domain specific designs for a particular application
what varies and separate varying parts from what does not change Design Principles In general: • Program to an interface, not an implementation • “HAS A…” is better than “IS A…” Favour composition over inheritance • Depend upon abstractions. Do not depend on concrete classes. Dependency Inversion Principle
each one and make them interchangeable. • Also known as “Policy” • A behavioural design pattern • Incredibly common with DI being ubiquitous these days • Example • Process a list of first names in order to send out marketing emails. • Make it easy to switch algorithms for creating greetings – this is currently is currently hard coded. • The Strategy Pattern will allow the algorithm vary independently from the clients that use it.
into another interface that clients expect, allowing them to work together. • Also known as a wrapper • Structural design pattern • Example • Ducks vs turkeys • We’ve run out of ducks, so we’re going to use turkey instead • A sophisticated interface “IDuck” interface already exists. We are going to wrap an “ITurkey” to implement it so that they can be used seamlessly.
use an existing class, but the interface does not match the one you need • You need to work with unforeseen interfaces in 3rd party code and/or you want to stay true to the dependency inversion principle • You want to reuse several existing subclasses but it’s impractical to adapt each one • Create an adapter that wraps the base class