Lab14 - Design Patterns (Observer&Decorator)
Lab14 - Design Patterns (Observer&Decorator)
Lab 14
Content
• Observer Pattern
• Decorator Pattern
2
Observer Pattern
• GoF Intent: “Defines a one-to-many dependency between objects
so that when one object changes state, all of its dependents are
notified and updated automatically.”
3
Observer Pattern
• Definitions of Observer Pattern
– Subject
• Sends a notification message to one or many observers when there is a
change in state.
• Along with the message, provides information on the state that has changed.
– Observers
• The objects that want to be notified about changes in the Subject’s state
4
Observer Pattern
• Power of loose coupling
– The only thing the subject knows about an observer is that it implements
a certain interface.
– We can add new observers at any time.
– We never need to modify the subject to add new types of observers.
– We can reuse subjects or observers independently of each other.
– Changes to either the subject or an observer will not affect the other.
5
Observer Pattern
• Observer Pattern (Publishers + Subscribers)
6
Observer Pattern
• NumberGenerator에서 생성된 값을 Digital과 Graph로 표시하기
– NumberGenerator에서 값을 생성하면 Digital과 Graph로 화면에 출력하는
어플리케이션이다.
– 목표는
• NumberGenerator에서 랜덤 값을 생성하면, 화면에 Digital과 Graph로 표시되도
록 하는 것이다. (Ex. Digital : 5, Graph : *****)
7
Observer Pattern
• 예제 프로그램의 클래스 다이어그램
Notifies <<interface>>
NumberGenerator Observer
observers
update
addObserver
deleteObserver
notifyObservers
getNumber
execute DigitObserver GraphObserver
update update
RandomNumberGenerator
random
number
getNumber
execute
8
Observer Pattern
• Observer Interface
9
Observer Pattern
• NumberGenerator Class
10
Observer Pattern
RandomNumberGenerator Class
11
Observer Pattern
• DigitObserver Class
12
Observer Pattern
• GraphObserver Class
13
Self-Test
• 랜덤 숫자를 표시하는 프로그램을 구현하여 동작하도록 한다.
– 8 페이지의 다이어그램을 이용하여 제공된 코드의 빈 부분을 구현한다.
• NumberGenerator 클래스
– 등록되어 있는 Observer에게 최신 값을 update 해야 한다.
• RandomNumberGenerator 클래스
– 값이 바뀔 때 마다 NumberGenerator 클래스를 이용해 notify 해야 한다.
• DigitObserver
– 생성자에서 Subject에 해당 Observer 객체를 등록해야 한다.
• GraphObserver
– 생성자에서 Subject에 해당 Observer 객체를 등록해야 한다.
14
Self-Test (Contd)
• 출력 결과는 아래와 같다.
15
Observer Pattern
• Weather Monitoring Application
16
Observer Pattern
• Weather Monitoring Application
– Weather Station which monitors humidity, temperature, and pressure.
– There are three different displays for showing the information from the
Weather Station
• Current Conditions
• Weather Stats
• Forecast
– Goal:
• Changes in the Weather Station must update all three displays.
• In addition, need capability to add additional displays in the future.
17
Observer Pattern
• WeatherData Class
WeatherData
getTemperature()
getHumidity()
getPressure()
// …
measurementsChanged() /*
* This method gets called
// Other methods
* whenever the weather measurements
* have been update.
*/
18
Observer Pattern
• WeatherData Class Implementation
19
Observer Pattern
• What’s Wrong with the Implementation
③
②
20
Observer Pattern
• Designing the Weather Station
21
Observer Pattern
• Implementing the Weather Station
22
Observer Pattern
• Implementing the Weather Station
23
Observer Pattern
• Implementing the Weather Station
24
Observer Pattern
• Implementing the Weather Station
25
Observer Pattern
• Implementing the Weather Station
26
Decorator Pattern
• Decorator Pattern
– Decorator pattern dynamically adds additional requirements to objects.
Decorator provide a method to extend functionality flexibly by creating
subclasses.
27
Decorator Pattern
• Starbuzz Coffee
28
Decorator Pattern
• Starbuzz Coffee (Introduction to Decorator)
29
Decorator Pattern
• Starbuzz Coffee (Introduction to Decorator)
– The Open-Closed Principle: “Classes should be open for extension, but
closed for modification.”
• Our goal is to allow classes to be easily extended to incorporate new
behavior without modifying existing code.
• We’re going to strive to design our system so that the closed parts are
isolated from our new extensions.
– Designs that are resilient to changes and flexible enough to take on new
functionality to meet changing requirements
30
Decorator Pattern
• Meet the Decorator Pattern
– We’ll start with a beverage and “decorate” it with the condiments at
runtime.
– For example, if the customer wants a Dark Roast with Mocha and Whip,
then we’ll:
• Take a DarkRoast object
• Decorate it with a Mocha object
• Decorate it with a Whip object
• Call the cost() method and rely on delegation to add on the condiment costs
31
Decorator Pattern
• Meet the Decorator Pattern
– We can compute the cost by calling cost() on the outermost decorator,
Whip, and Whip is going to delegate computing the cost to the objects
it decorates. Once it gets a cost, it will add on the cost of the Whip.
32
Decorator Pattern
• Writing the Starbuzz code
33
Decorator Pattern
• Writing the Starbuzz code
34
Decorator Pattern
• Writing the Starbuzz code
35
Decorator Pattern
• Writing the Starbuzz code
36
Decorator Pattern
• Real World Decorators: Java I/O
37
Self-Test
• 제공된 StarBuzz 코드를 수정하여 아래와 같이 동작하도록 한다.
– StarBuzz에 새로운 메뉴를 추가한다.
• Hyper Dark Roast Coffee를 추가한다. (가격 : $2.00)
• Cinnamon을 첨가물로 추가 한다. (가격 : $0.50)
38
Adapter Pattern (범위 X)
• The Adapter Pattern converts the interface of a class into another
interface the clients expect.
39
Adapter Pattern (Contd)
40
Adapter Pattern (Contd)
41
Adapter Pattern (Contd)
42
Adapter Pattern (Contd)
43