0% found this document useful (0 votes)
79 views

Observer & Decorator: Patterns

The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified automatically. An object acts as the subject that holds the core data, while observers register with the subject and are notified of any state changes. The decorator pattern allows adding new behaviors to individual objects at runtime by wrapping them in decorator classes that contain the added functionality.

Uploaded by

Willy Wonka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Observer & Decorator: Patterns

The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified automatically. An object acts as the subject that holds the core data, while observers register with the subject and are notified of any state changes. The decorator pattern allows adding new behaviors to individual objects at runtime by wrapping them in decorator classes that contain the added functionality.

Uploaded by

Willy Wonka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

OBSERVER

&
DECORATOR
PATTERNS
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.
Observer Pattern: Structure
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.
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.
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).
Observer Pattern: Answer
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.
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.


Decorator Example
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
Decorator : Answer
public class CarDecorator implements Car {
protected Car car;
public CarDecorator(Car c){
this.car=c;
}

@Override
public void assemble() {
this.car.assemble();
}
}
Decorator : Answer
public class SportsCar extends CarDecorator{ public class LuxuryCar extends CarDecorator{
public SportsCar(Car c) { public LuxuryCar(Car c) {
super(c); super(c);
} }

@Override @Override
public void assemble(){ public void assemble(){
car.assemble(); car.assemble();
System.out.print(" Adding features System.out.print(" Adding
of Sports Car."); features of Luxury Car.");
} }
} }
Decorator : Answer
public class DecoratorPatternClass{

public void createCar() {


Car sportsCar= new SportsCar(new BasicCar());
sportsCar.assemble();
Car luxurySportsCar= new LuxuryCar(sportsCar);
luxurySportsCar.assemble();
}

You might also like