The Observer Pattern: Software Design - CS 350 - Prof. Jeff K. Salvage, Study notes of Computer Science

The observer pattern is a software design technique that enables a one-to-many dependency between objects, allowing the subject to notify its dependents (observers) automatically when its state changes. The intent, problem, solution, and implementation of the observer pattern, along with an example using java.

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-n9e
koofers-user-n9e 🇺🇸

8 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 350
CS 350
Software Design
Software Design
The Observer Pattern
The Observer Pattern
Chapter 18
Chapter 18
Let
Let
s expand the case study to include new features:
s expand the case study to include new features:
Sending a welcome letter to new customers
Sending a welcome letter to new customers
Verify the customer
Verify the customer
s address with the post office
s address with the post office
In an ideal world, we know all the requirements and they will no
In an ideal world, we know all the requirements and they will not change.
t change.
If they don
If they don
t change, they can be hard coded into the Customer object as sho
t change, they can be hard coded into the Customer object as shown below:
wn below:
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download The Observer Pattern: Software Design - CS 350 - Prof. Jeff K. Salvage and more Study notes Computer Science in PDF only on Docsity!

CS 350

CS 350 –

– Software Design

Software Design

The Observer Pattern

The Observer Pattern –

– Chapter 18

Chapter 18

Let

Let’

’s expand the case study to include new features:

s expand the case study to include new features:

Sending a welcome letter to new customers

Sending a welcome letter to new customers

Verify the customer

Verify the customer’

’s address with the post office

s address with the post office

In an ideal world, we know all the requirements and they will no

In an ideal world, we know all the requirements and they will not change.

t change.

If they don

If they don’

’t change, they can be hard coded into the Customer object as sho

t change, they can be hard coded into the Customer object as shown below:

wn below:

CS 350

CS 350 –

– Software Design

Software Design

The Observer Pattern

The Observer Pattern –

– Chapter 18

Chapter 18

Class

Class

Responsibility

Responsibility

Customer

Customer

When a customer is added, this object will make the calls to the

When a customer is added, this object will make the calls to the other

other

objects to have the corresponding actions take place

objects to have the corresponding actions take place

WelcomeLetter

WelcomeLetter

Creates welcome letters for customers that let them know they we

Creates welcome letters for customers that let them know they were

re

added to the system.

added to the system.

AddrVerification

AddrVerification

This object will verify the address of any customer that asks it

This object will verify the address of any customer that asks it to

to

CS 350

CS 350 –

– Software Design

Software Design

The Observer Pattern

The Observer Pattern –

– Chapter 18

Chapter 18

Objects that must be notified are called

Objects that must be notified are called Observers

Observers.

All observers must have the same interface.

All observers must have the same interface.

All Observers must register themselves. This makes them responsi

All Observers must register themselves. This makes them responsible for knowing what they

ble for knowing what they

are watching for. This also frees the subject from knowing which

are watching for. This also frees the subject from knowing which observers depend

observers depend

on it.

on it.

We must add two methods to the subject.

We must add two methods to the subject.

attach(Observer

attach(Observer) adds the given observer to its list of observers.

) adds the given observer to its list of observers.

detatch(Observer

detatch(Observer) removes the given observer from its list of observers.

) removes the given observer from its list of observers.

With the observers registered it is a simple matter for the subj

With the observers registered it is a simple matter for the subject to notify the Observers

ect to notify the Observers

when an event occurs.

when an event occurs.

The observer must implement a method called update.

The observer must implement a method called update.

The subject implements the notify method that goes through its l

The subject implements the notify method that goes through its list of Observers and calls

ist of Observers and calls

this update method for each of them.

this update method for each of them.

CS 350

CS 350 –

– Software Design

Software Design

The Observer Pattern

The Observer Pattern –

– Chapter 18

Chapter 18

This is not enough.

This is not enough.

In many cases the Observers need more information than just that

In many cases the Observers need more information than just that the event occurred.

the event occurred.

To solve this the subject must contain additional methods that c

To solve this the subject must contain additional methods that can be called by the Observer

an be called by the Observer

so that the observer can obtain the necessary information.

so that the observer can obtain the necessary information.

CS 350

CS 350 –

– Software Design

Software Design

The Observer Pattern

The Observer Pattern –

– Chapter 18

Chapter 18

Observer Code

Observer Code

interface interface

MyObserverMyObserver

void

update

(Custom

void

update

(Custom

myCust myCust);

class class

AddrVerificationAddrVerification

implement implement

MyObserverMyObserver

publicpublic

AddrVerificationAddrVerification()

Public

void

update

(Customer

Public

void

update

(Customer

myCust myCust)

//do//do

AdddressAdddress

Verification

stuff

here

Verification

stuff

here

class class

WelcomeLetterWelcomeLetter

implements implements

MyObserver MyObserver

publicpublic

WelcomeLetterWelcomeLetter

public

void

update

(Customer

public

void

update

(Customer

myCustmyCust)

do

Welcome

Letter

Stuff

here

do

Welcome

Letter

Stuff

here

CS 350

CS 350 –

– Software Design

Software Design

The Observer Pattern

The Observer Pattern –

– Chapter 18

Chapter 18

What happens when we add new observers?

What happens when we add new observers?

Imagine adding the ability to send a letter with coupons to cust

Imagine adding the ability to send a letter with coupons to customers located within 20

omers located within 20

miles of one of the company

miles of one of the company’

’s

s “

“Brick and Mortar

Brick and Mortar”

” stores.

stores.

We just need to add a new observer that send a coupon.

We just need to add a new observer that send a coupon.

CS 350

CS 350 –

– Software Design

Software Design

The Observer Pattern

The Observer Pattern –

– Chapter 18

Chapter 18

The Observer Pattern: Key Features

The Observer Pattern: Key Features

Intent: Define a one

Intent: Define a one-

-to

to-

-many dependency between objects so that when one object changes

many dependency between objects so that when one object changes

state, all its dependents are notified and updated automatically

state, all its dependents are notified and updated automatically.

Problem: You need to notify a varying list of objects that an ev

Problem: You need to notify a varying list of objects that an event has occurred.

ent has occurred.

Solution: Observers delegate the responsibility for monitoring f

Solution: Observers delegate the responsibility for monitoring for an event to a central

or an event to a central

object: The subject.

object: The subject.

Implementation: Have objects (Observers) that want to know when

Implementation: Have objects (Observers) that want to know when an event happen attach

an event happen attach

themselves to another object (Subject) that is watching for the

themselves to another object (Subject) that is watching for the event to occur or that

event to occur or that

triggers the event itself.

triggers the event itself.

When the event occurs, the Subject tells the Observers that it h

When the event occurs, the Subject tells the Observers that it has occurred.

as occurred.

The Adapter pattern is sometimes needed to be able to implement

The Adapter pattern is sometimes needed to be able to implement the

the

Observer interface for all the Observer

Observer interface for all the Observer-

-type objects.

type objects.

CS 350

CS 350 –

– Software Design

Software Design

The Observer Pattern

The Observer Pattern –

– Chapter 18

Chapter 18

Generic Structure Observer Pattern

Generic Structure Observer Pattern