S10 - Design Patterns
S10 - Design Patterns
S10 - Design Patterns
Design Patterns
Memi Lavi
www.memilavi.com
Design Patterns:
A collection of general, reusable solutions to
common problems* in software design
* Examples:
• How to communicate between classes
• How to initialize interface implementations
• How to access data stores
• And more…
Benefits of Using Design Patterns
• Introduced in 1987
• Factory
• Repository
• Façade
• Command
Factory Pattern
Factory Pattern
Creating objects without specifying the exact
class of the object
Factory Pattern Motivation
New Is Glue
Factory Pattern Example - Weather
Factory Pattern Example - Weather
More Types of Factory Pattern
Factory Pattern
• Hugely popular
• Create
• UpdateName
• Delete
Repository Pattern - Example
Repository Pattern - Usage
Repository Pattern - Usage
Repository Pattern – Data Store Change
Repository Pattern
• Generic classes
• Inheritance
• Extension Frameworks
• Banking application
• Transfer money
• Make sure accounts exist
• Make sure the first account has enough money
• Withdraw money from first account
• Deposit money in second account
• Add event in account log
Façade Pattern - Example
Façade Pattern - Usage
Command Pattern
Command Pattern
All the action’s information is encapsulated
within an object
Command Pattern - Example
• Undo mechanism
• Naïve implementation:
This is
Bad!
Command Pattern - Usage
1. ICommand Interface:
2. Command Classes:
…
Command Pattern - Usage
Command
Object
Receiver
Command Pattern - Usage
Invoker
Design Patterns - Summary
• Factory
• Repository
• Façade
• Command
Design Patterns - Summary
maintain software