5. A design pattern is a general reusable
solution to commonly occurring problems
within a given context in software design.
What are design patterns?
6. ● Initiated by Kent Beck &
Ward Cunningham
● Gang of Four wrote - Design
Patterns: Elements of Reusable
Object-Oriented Software
● Sold 500k books and 13
languages
Where did they come from?
8. Creational Patterns
Aim to separate a system from how its objects are created,
composed, and represented.
Factory
Lazy Initialization
Prototype
Singleton
Have you seen these?
9. Use inheritance to compose interfaces and define ways to
compose objects to obtain new functionality
● Adapter
● Composite
● Decorator
● Proxy
Any experience with these?
Structural Patterns
10. Behavioral Patterns
Specifically concerned with the communication between objects.
● Template Method
● Observer
● Strategy
● Command
Do we use any of these at Jane.com?
12. Template Method: Encapsulate and Extend Algorithms
An algorithm is a step-by-step procedure for accomplishing some
end.
We follow millions of learned algorithms daily. One high level
algorithm that I do is called my morning routine. Let’s code it!
Create a new file in your language of choice.
Taken from blog - http://bit.ly/1TTrNwa
13. Morning Algorithm
1. Wake-up
2. Work out
3. Eat Breakfast
4. Drive to work
● execute() - Template Method
○ Encapsulates the steps
○ Executes without knowing
method implementation
1. wakeUp()
2. workOut()
3. eatBreakfast()
4. driveToWork()
Pseudo Code
14.
15. Late Morning Algorithm
1. Wakeup
2. Skip Workout
3. Eat Fast Breakfast
4. Drive to work
● Inherit from MorningAlgorithm
● workOut()
○ Skip
● eatBreakfast()
○ Eat something quick
● driveToWork()
lma = new LateMorningAlgorithm()
lma.execute();
Pseudo Code
16.
17. Is This Really Practical?
● Generating Emails
○ Fetch Data, Generate HTML, Send to Email Service
● Website Scraping
○ Download, parser, format data, export
● iOS ViewController Lifecycles
○ viewWillAppear, viewDidAppear
19. Caveat 1: Speculative Design
Trying to use all the patterns is a bad thing, because you will end
up with synthetic designs—speculative designs that have
flexibility that no one needs. These days software is too complex.
We can’t afford to speculate what else it should do. We need to
really focus on what it needs.” – Erich Gamma (GoF)
20. You Ain’t Gonna Need It
Keep It Simple Stupid!
4 Design Pattern to say in Hello World
http://bit.ly/1MkvyVJ
Caveat 2: YAGNI & KISS
21. Caveat 3: Language Suitability
Patterns that imply mutable state may be unsuited for functional
programming languages, some patterns can be rendered
unnecessary in languages that have built in support for solving
the problem they are trying to solve, and object-oriented
patterns are not necessary suitable for non-object-oriented
languages.