Observer & Decorator Pattern - University of California ...€¦ · Observer Pattern Define a...

14
OBSERVER & DECORATOR PATTERNS

Transcript of Observer & Decorator Pattern - University of California ...€¦ · Observer Pattern Define a...

Page 1: Observer & Decorator Pattern - University of California ...€¦ · Observer Pattern Define a one-to-many dependency between objects so that when one object changes state, all its

OBSERVER &

DECORATOR PATTERNS

Page 2: Observer & Decorator Pattern - University of California ...€¦ · Observer Pattern Define a one-to-many dependency between objects so that when one object changes state, all its

Observer Pattern

■ Define a one-to-many dependency between objects so that when one object changes state, all its

dependents are notified and updated automatically.

■ Define an object that is the "keeper" of the data model and/or business logic (the Subject).

■ Delegate all "view" functionality to decoupled and distinct Observer objects.

■ Observers register themselves with the Subject as they are created. Whenever the Subject changes, it

broadcasts to all registered Observers that it has changed, and each Observer queries the Subject for

that subset of the Subject's state that it is responsible for monitoring.

Page 3: Observer & Decorator Pattern - University of California ...€¦ · Observer Pattern Define a one-to-many dependency between objects so that when one object changes state, all its

Observer Pattern: Structure

Page 4: Observer & Decorator Pattern - University of California ...€¦ · Observer Pattern Define a one-to-many dependency between objects so that when one object changes state, all its

Observer Pattern: Example

■ Each bidder possesses a numbered paddle that

is used to indicate a bid.

■ The auctioneer starts the bidding, and "observes"

when a paddle is raised to accept the bid.

■ The acceptance of the bid changes the bid price

which is broadcast to all of the bidders in the

form of a new bid.

Page 5: Observer & Decorator Pattern - University of California ...€¦ · Observer Pattern Define a one-to-many dependency between objects so that when one object changes state, all its

Observer Pattern: Benefits

■ Allows the number and "type" of "view" objects to be configured dynamically, instead of being

statically specified at compile-time.

■ It is a "pull" interaction model. Instead of the Subject "pushing" what has changed to all

Observers, each Observer is responsible for "pulling" its particular "window of interest" from the

Subject.

■ At the discretion of the designer, include: implementing event compression (only sending a single

change broadcast after a series of consecutive changes has occurred), having a single Observer

monitoring multiple Subjects, and ensuring that a Subject notify its Observers when it is about to

go away.

Page 6: Observer & Decorator Pattern - University of California ...€¦ · Observer Pattern Define a one-to-many dependency between objects so that when one object changes state, all its

Observer Pattern: Question (Take 10 mins)

A news agency gathers news and publishes them to different subscribers. We need to

create a framework for the agency to be able to inform immediately, when an event

occurs, its subscribers about the event. The subscribers can receive the news in

different ways: Emails, SMS, ... The solution needs to be extensive enough to support

new types of subscribers (maybe new communication technologies will appear).

Page 7: Observer & Decorator Pattern - University of California ...€¦ · Observer Pattern Define a one-to-many dependency between objects so that when one object changes state, all its

Observer Pattern: Answer

Page 8: Observer & Decorator Pattern - University of California ...€¦ · Observer Pattern Define a one-to-many dependency between objects so that when one object changes state, all its

Decorator Pattern

■ You want to add behavior or state to individual objects at run-time.

■ The Decorator attaches additional responsibilities to an object dynamically.

■ The ornaments that are added to Christmas trees are examples of Decorators.

Page 9: Observer & Decorator Pattern - University of California ...€¦ · Observer Pattern Define a one-to-many dependency between objects so that when one object changes state, all its

Intent

■ This pattern creates a decorator class which wraps the original class and

provides additional functionality keeping class methods signature intact.

■ Client-specified embellishment of a core object by recursively wrapping it.

■ Wrapping a gift, putting it in a box, and wrapping the box.

Page 10: Observer & Decorator Pattern - University of California ...€¦ · Observer Pattern Define a one-to-many dependency between objects so that when one object changes state, all its

Decorator Example

Page 11: Observer & Decorator Pattern - University of California ...€¦ · Observer Pattern Define a one-to-many dependency between objects so that when one object changes state, all its

Decorator: Question (Take 8 mins)

Now based on these definitions, how would you design the following-

1) A luxury car

2) A sports car

3) A luxury car that is also a sports car

Page 12: Observer & Decorator Pattern - University of California ...€¦ · Observer Pattern Define a one-to-many dependency between objects so that when one object changes state, all its

Decorator : Answer

public class CarDecorator implements Car {

protected Car car;

public CarDecorator(Car c){

this.car=c;

}

@Override

public void assemble() {

this.car.assemble();

}

}

Page 13: Observer & Decorator Pattern - University of California ...€¦ · Observer Pattern Define a one-to-many dependency between objects so that when one object changes state, all its

Decorator : Answer

public class SportsCar extends CarDecorator{

public SportsCar(Car c) {

super(c);

}

@Override

public void assemble(){

car.assemble();

System.out.print(" Adding features of Sports Car.");

}

}

public class LuxuryCar extends CarDecorator{

public LuxuryCar(Car c) {

super(c);

}

@Override

public void assemble(){

car.assemble();

System.out.print(" Adding features of Luxury Car.");

}

}

Page 14: Observer & Decorator Pattern - University of California ...€¦ · Observer Pattern Define a one-to-many dependency between objects so that when one object changes state, all its

Decorator : Answer

public class DecoratorPatternClass{

public void createCar() {

Car sportsCar= new SportsCar(new BasicCar());

sportsCar.assemble();

Car luxurySportsCar= new LuxuryCar(sportsCar);

luxurySportsCar.assemble();

}

}