Download presentation
Presentation is loading. Please wait.
Published byLinette Lester Modified over 6 years ago
1
Design Patterns Part 1: Observer, Singleton, & Adapter
2
Learning Objectives Design patterns: what & why When and how to apply
Familiarity with several popular patterns: Observer Singleton Adapter
3
What is a Design Pattern?
Template solutions to common problems Benefits: Avoid re-inventing solutions Promotes software quality Reusability, maintainability, etc. Facilitate communication
4
Running Example: Tiger Dining
5
Tiger Dining Web App
7
Design problem: notification
8
Chef’s view
9
Manager’s view: To see subscribed users
10
How to connect? Orders notification
11
OrdersController new() create() … Notifier order_notification() ?
12
Solution: Observer Pattern
Subject attach(observer) detach(observer) notify() Observer update() observes * Needs to add an observer for the food order activity Define one-to-many dependency
13
order_notification() update()
Subject attach(observer) detach(observer) notify() Observer update() observes * OrdersController new() create() add_observer() notify_observers() Notifier order_notification() update() observes *
14
Control Flow OrdersController Order EmailNotifier create save
notify_observers Left to right update Send
15
Goal: Implement Observer pattern
Modify OrdersController (Subject) include “Observable” adds observer using “add_observer” Uses “notify_observers” Modify Notifier (Observer) Observer has “update” method Add code snippet
16
Goal: Implement Observer pattern
17
Goal: Implement Observer pattern
18
Other Examples Spreadsheet Employee payroll GUI program (callback)
19
Classroom Activity
20
Design problem: Sales Logging
21
Problem Saves records of transactions as backup
Sales logging for transaction failure
22
SalesLogger initialize(file) formatter log(message)
23
Solution: Singleton Single file to log all the sales information
Easy to look up Singleton inst: Singleton instance(): Singleton … if inst == nil inst = Singleton.new end return inst
24
Making class Singleton
Steps of making a class Singleton: Creating the class variable Initializing it with the single instance = SimpleLogger.new Creating the class level instance method Making a new private
25
Goal: Implement Singleton pattern
SalesLogger class include “Singleton” SalesController call instance call log(message)
26
Goal: Implement Singleton pattern
27
Singleton Examples In Rails: Outside Rails: development.log test.log
Webrick in rake Outside Rails: tomcat ant
28
Design problem: New Payment method
29
Goal: To Add Visa Debit Card and Paypal
30
Goal: To add VisaPay PaymentsController new create … DiningDollars
charge(uid) …
31
Solution: Adapter Interface which bridge a gap in payment methods
Key objects are Target, Adapter and Adaptee Target Request() Client Adapter Request() Adaptee SpecificRequest()
32
DiningDollarsAdapter
PaymentProcessor process_payment() PaymentsController DiningDollarsAdapter process_payment() VisaPayAdapter process_payment() … DiningDollars charge(uid) VisaPay pay(debitcard, cvv, exp) …
33
Visa processes the payment
Control Flow PaymentsController VisaPayAdapter VisaPay create process_payment pay Visa processes the payment
34
Goal: Implement Adapter Pattern
PaymentsController “create” method call the adapter based on the input type VisaPayAdapter defines “process_payments” inherited from the PaymentProcessor VisaPay defines pay(creditcard, cvv, exp) saves the information
35
Adapter: Example in Rails
Active Records Contains adapters for several databases (PostgreSQL or MySQL) Wraps them up in common interface AbstractAdapter implements common functionality PostgreSQLAdapter AbstractMysqlAdapter
36
Summary Singleton: Observer: Adapter: Making Sure There Is Only one
Keeping Up with the Times Adapter: Filling the gaps
37
Practice Quiz
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.