Design Patterns
Design Patterns
Introduction
The Why
Software
A
Software
C
How do I make
“Way of Thinking” my classes more
flexible?
About Software Design (Code)
GamePlay
Scoreboard
Facade
Adapter
Factory
Decorator
Observer
Strategy
Singleton
The Gang of Four
The Creators of Design Patterns
The Strategy Design
Pattern
Algorithms for Different Cases…
ScoreBoard
calculateScore()
showScore()
JDK
Example # 2
JDK
The Observer Design
Pattern
Observer Design Pattern
… defines a 1-to-many
dependency between objects
so that when one object changes
state, all of its dependents are
notified and updated automatically
Observer Pattern Example
JDK
Problem with Current State…
Project grows - more JAR Files
JDK
More JAR files - more class path
issues
JAR Files
JDK
Ice-cream Shop
JDK
Problem…
JDK
Solution
JDK
The Result…
Chocolate
cost() cost() will give us the final total cost
Vanilla
cost()
JDK
We need more than 1 type of burger…
Solution - Simple Factory
JDK
Problem solved… (For now)
JDK
Solution - Class Diagram
JDK
The Factory Pattern*
MozHamburgerFactory
JDK
HamburgerStore
JamHamburgerFactory
Create our Franchises…
MozHamburgerFactory
JDK
HamburgerStore
JamHamburgerFactory
The Singleton Pattern
Object Creation
JDK
Calling
CheeseBurger()
public constructor
object
1 object object3
2
object object
4 …
Motivation: Too Many Object…
JDK
Solution
Game Console
The Command Pattern System
Invoker
JDK
Definition…
JDK
Computer
Monitor Mouse ….
Too complex….
UML Facade Diagram
Definition…
JDK
Redundant…..NOT Good!
Solution…
JDK
The Template Method
Pattern
Definition…
Computer
Monitor Mouse ….
Too complex….
UML Facade Diagram
Facade - Definition
Definition…
JDK
Inventory/Products Inventory/Products
ArrayList Array
Problem
Inventory/Products
Inventory/Products
ArrayList Array
Iterating through Products…
Inventory/Products
Inventory/Products
ArrayList Array
Obvious Solution
Iterator
JDK
JDK
The Result…
Chocolate
cost() cost() will give us the final total cost
Vanilla
cost()