Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Design Patterns Club

Design Patterns Club

A series of tutorials to teach object oriented language design patterns, based on the GOF book.

Paul McGrath

June 10, 2017
Tweet

More Decks by Paul McGrath

Other Decks in Programming

Transcript

  1. Introduction •“There is nothing new, except what has been forgotten.”

    • 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
  2. Introduction • The book • Object oriented software design •

    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
  3. What is a design pattern? • Descriptions of communicating classes

    that are customised to solve a common design problem in a particular context • Pattern name • Problem • Solution • Consequences
  4. The basis of all Design Patterns: • Identify and encapsulate

    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
  5. Design Pattern Space Purpose Creational Structural Behavioural Scope Class Factory

    Method Adapter (class) Template Method Object Abstract Factory Adapter (object) Visitor Builder Decorator Strategy Composite Observer
  6. Strategy • Intent • Define a set of algorithms, encapsulate

    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.
  7. Strategy Exercise • The client code currently hard codes one

    of the algorithms: • Add a common interface to each of the algorithms and inject an object of that interface into the constructor of the client
  8. Adapter • Intent • Adapts functionality in an existing class

    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.
  9. Adapter • You will need write an adapter if you

    want to use 3rd party code without changing your code
  10. Adapter - Exercise • Write a new TurkeyAdapter class that

    implements IDuck • It will need to wrap an ITurkey in order to delegate to • Do this by injecting an ITurkey into the constructor
  11. Adapter - Use • Use when: • You want to

    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