2. Kaya Weers | Ordina
First, an important question..
3. Kaya Weers | Ordina
So… Design Patterns?
Design patterns don’t give you code.
They provide general, reusable solutions for the common
problems in software design
Blueprints that you can customize.
4. Kaya Weers | Ordina
A bit of history
• Book by the Gang of Four (1994)
• 23 patterns, in 3 categories
• Ways to solve problems
• Same vocabulary
5. Kaya Weers | Ordina
Let me introduce you:
Bobby
Billie
9. Kaya Weers | Ordina
So, what’s the pattern?
• Mom wants groceries
• She writes her request on a note: what needs to be
done by who.
• Little sister’s tasks is to bring the note to the table
• Bobby knows how to handle the request on the note
10. Kaya Weers | Ordina
Command pattern
• Encapsulates request as a stand-alone object
• Decouple trigger from the content request
• Invoker doesn’t need to know content of request
11. Kaya Weers | Ordina
Command pattern
(client) (invoker) (Command)
(receiver)
Mom Little Sister Note
DoGroceries
Note
Bobby
interface
setCommand(note) execute()
execute()
doGroceries()
12. Kaya Weers | Ordina
* constructors omitted for readability *
13. Kaya Weers | Ordina
Usage in the software world
• Queue or schedule
• Reversible operations
17. Kaya Weers | Ordina
So, what’s the pattern?
• Bobby asks employee when the coffee will be back in
stock
• Employee doesn’t know at that moment
• Employee doesn’t want customers to ask him again
• Customers can sign up to be notified as soon as the
product is available
18. Kaya Weers | Ordina
Observer pattern
• One-to-many relationship
• When the state from an object changes,
all dependencies are notified
• Loose coupling
19. Kaya Weers | Ordina
Observer pattern
(subject)
(Observers)
StockUpdate
Bobby
Customer 1
Customer 2
register(observer)
20. Kaya Weers | Ordina
Observer pattern (Observers)
StockUpdate
Bobby
Customer 1
Customer 2
notifyObservers()
update()
update()
update()
(subject)
21. Kaya Weers | Ordina
* constructors omitted for readability *
22. Kaya Weers | Ordina
Usage in the software world
• Dynamic composition of objects at runtime
• No control over who gets information
23. Kaya Weers | Ordina
The next product..
.. is also a challenge
24. Kaya Weers | Ordina
But, the text is in..
Spanish!
26. Kaya Weers | Ordina
So, what’s the pattern?
• Billie wants to read the ingredients
• But Billie can’t read Spanish
• Translation app converts the Spanish words to
English words
• Billie can read English
27. Kaya Weers | Ordina
Adapter pattern
• Lets classes work together that couldn’t otherwise,
because of incompatible interfaces
• The adapter converts an interface into an interface
that the client expects
• More generic interface
33. Kaya Weers | Ordina
They enter the French bakery
34. Kaya Weers | Ordina
So, what’s the pattern?
• Order supermarket bakery: ‘bread’
• Order French bakery: ‘bread’
• Different result: different variants of bread
• Creation differs per location
35. Kaya Weers | Ordina
Factory method pattern
• Interface for creating objects in superclass
• Object creation in subclass, can alter the type of
objects
• Easy to extend: add new creator subclasses
36. Kaya Weers | Ordina
Factory method pattern
Bread
WholeGrain
SlicedBread
interface
Baguette
Bakery
Supermarket
Bakery
createBread()
createBread()
abstract class
French
Bakery
createBread()
factory method
37. Kaya Weers | Ordina
* constructors omitted for readability *
38. Kaya Weers | Ordina
Usage in the software world
• Don’t know all the types beforehand
• Extending library
39. Kaya Weers | Ordina
The smell of the bakery
.. made them hungry.
40. Kaya Weers | Ordina
The baker prepares the sandwiches
42. Kaya Weers | Ordina
So, what’s the pattern?
• Bobby and Billie order two different sandwiches
• The baker creates them with different ingredients
• Most of the steps are the same, easy for baker
• Some steps are optional, like the topping
43. Kaya Weers | Ordina
Template method pattern
• Superclass defines a skeleton
• Subclasses have specific implementation for steps
• Superclass can contain (default) implementation
50. Kaya Weers | Ordina
So, what’s the pattern?
• Mom gives 1 instruction: “put away groceries”
• Bobby and Billie put everything in the right place
• Mom gives no specific instructions
• Mom doesn’t need to know about steps taken
51. Kaya Weers | Ordina
Facade pattern
• Simplified interface for client
• Client doesn’t need to know about subsystem
• Removes complexity from the interface
52. Kaya Weers | Ordina
Facade pattern
Bobby
Freezer
putAwayGroceries()
Cabinet Refrigerator Pantry
(client)
Mom
(facade)
53. Kaya Weers | Ordina
* constructors omitted for readability *
54. Kaya Weers | Ordina
Usage in the software world
• Home automation
• Multiple facades for complex subsystem