POLA-POLA PERANCANGAN (PPP) Behavioral pattern: Observer

POLA-POLA PERANCANGAN (PPP) Behavioral pattern: Observer Tujuan perkuliahan • Memahami behavioral pattern: Observer Bahan Kuliah PPP - Observer pat...
Author: Mervin Newton
3 downloads 0 Views 3MB Size
POLA-POLA PERANCANGAN (PPP) Behavioral pattern: Observer

Tujuan perkuliahan • Memahami behavioral pattern: Observer

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

2/25

Intent and Known As •

Intent: – Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically • Know As: – Dependents, Publish-Subscribe

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

3/25

Motivation

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

4/25

Motivation (1)

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

5/25

Motivation (2) • The WeatherData class has getter methods for three measurement values: temperature, humidity and barometric pressure. • The measurementsChanged() method is called any time new weather measurement data is available. (We don’t know or care how this method is called; we just know that it is.) • We need to implement three display elements that use the weather data: a current conditions display, a statistics display and a forecast display. These displays must be updated each time WeatherData has new measurements. • The system must be expandable – other developers can create new custom display elements and users can add or remove as many display elements as they want to the application. Currently, we know about only the initial three display types (current conditions, statistics and forecast). Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

6/25

Motivation (3)

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

7/25

Motivation (4)

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

8/25

Applicability • When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independently. • When a change to one object requires changing others, and you don't know how many objects need to be changed. • When an object should be able to notify other objects without making assumptions about who these objects are. In other words, you don't want these objects tightly coupled. Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

9/25

Structure

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

10/25

Participants • Subject – knows its observers. Any number of Observer objects may observe a subject. – provides an interface for attaching and detaching Observer objects.

• Observer – defines an updating interface for objects that should be notified of changes in a subject.

• ConcreteSubject – stores state of interest to ConcreteObserver objects. – sends a notification to its observers when its state changes.

• ConcreteObserver – maintains a reference to a ConcreteSubject object. – stores state that should stay consistent with the subject's. – implements the Observer updating interface to keep its state consistent with the subject's.

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

11/25

Collaborations • ConcreteSubject notifies its observers whenever a change occurs that could make its observers' state inconsistent with its own. • After being informed of a change in the ConcreteSubject, aConcreteObserver object may query the subject for information. ConcreteObserver uses this information to reconcile its state with that of the subject.

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

12/25

Consequences • Vary subjects and observers independently  reuse subjects without reusing their observers, and vice versa • Abstract coupling between Subject and Observer  subject doesn't know the concrete class of any observer in its list • Support for broadcast communication – Freedom to add and remove observers at any time – Up to the observer to handle or ignore a notification

• Unexpected updates  simple update to Subject may cause a cascade of updates to Observer and its dependent objects

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

13/25

Sample code

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

14/25

Sample code (1)

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

15/25

Sample code (2)

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

16/25

Sample code (3)

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

17/25

Sample code (4)

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

18/25

Sample code (5)

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

19/25

Sample code (6)

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

20/25

Sample code (7): Java’s built-in

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

21/25

Sample code (8): Java’s built-in

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

22/25

Sample code (9): Java’s built-in

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

23/25

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

24/25

Sample code (11): Java’s built-in

Bahan Kuliah PPP - Observer pattern | Tri A. Kurniawan, S.T, M.T, Ph.D

25/25