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

OO Design Patterns: Session 2 Observer, Strategy, Decorator

This document discusses three design patterns: Observer, Decorator, and Strategy. The Observer pattern allows objects to monitor changes to other objects and receive notifications of changes. The Decorator pattern allows adding new behaviors to objects dynamically at runtime. The Strategy pattern allows algorithms to vary independently from the objects that use them.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

OO Design Patterns: Session 2 Observer, Strategy, Decorator

This document discusses three design patterns: Observer, Decorator, and Strategy. The Observer pattern allows objects to monitor changes to other objects and receive notifications of changes. The Decorator pattern allows adding new behaviors to objects dynamically at runtime. The Strategy pattern allows algorithms to vary independently from the objects that use them.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

OO Design Patterns

Session 2
Observer, Strategy, Decorator
Observer Pattern

 Or… How do I keep clients aware of


changes, when I’m not sure, or don’t
care, who they are?
Observer Pattern – High Level

 Category : Behavioral
 The Observer Pattern allows one or
more objects (Observer) to monitor
another object (Subject).
 A subset of the Pub-Sub Pattern
 When the state of one object changes,
dependants are notified
Observer Pattern

 OO Principals
 Promotes loose couplings
 The Subject does not need to be aware of
Observers except at runtime
 Favors composition over inheritance
 Program to an interface
Observer Pattern

 Basic Structure
 Observalable Subject
 1 or more Observers
Observer Pattern – Observer

 The Observer can register and


unregister itself with the Subject
 While registered any events the Subject
notifies on are received by all Observers
 The Observer can be programmed to an
interface that specifies a way for the
Subject to update it.
Observer Pattern – Observer
 The Observer is programmed 
to an interface that specifies a
way it to interact with the
Subject

public interface Observer {


public void
update( Subject o );
}
-OR-
public interface Observer {
public void
update( Subject o, Data a );
}
Observer Pattern – Subject
 The Subject maintains a list
of Observers
 The subject provides
access to Observers to
add or remove
themselves from a
distribution list
 When the Subject needs to
notify, it simply updates all
currently registered
Observers
Observer Pattern – SUN Impl

 Java has a number of Observer Pattern


implementation
 Swing
 General Purpose Observer
Observer Pattern – SUN Impl

 General Purpose Observer


 Java comes with an out of the box
implementation of the Observer Pattern
 The subject extends the java.util.Observable
class
 Observers implement the java.util.Observer

interface
 void update(Observable o, Object arg)
Observer Pattern – SUN Impl

 Some Things to Consider


 As mentioned this implementation forces us
to extend Observable for the subject
 Remember favor composition over inheritance
 This is implicitly single-threaded
 Synchronous
 As opposed to Asynch Pub/Sub
 Push/Pull
Observer Pattern

 Example
Observer Pattern

 Q&A
Decorator Pattern

 Or… How do I dynamically add


additional behaviors to an existing class?
Decorator Pattern – High Level

 Category : Structural
 The Decorator Pattern allows
responsibility and functionality to be
added dynamically at run-time
 Provides a flexible alternative to sub-
classing to extend functionality
Decorator Pattern

 OO Principals
 Encapsulate what varies
 Favor composition over inheritance

 Program to interfaces

 Strive for loosely coupled designs between


objects
 Open/closed principle
Decorator Pattern

 Open/closed principle
 Objects should be open for extension, but
closed for modification

 Allows an objects behavior to be modified


without altering its source
Decorator Pattern

 Basic Structure
 Abstract Component Class
 Concrete Components

 Abstract Decorator

 Concrete Decorator
Decorator Pattern

 Basic Structure
 Abstract Component Class
 A common ancestor that all involved objects
extend
Decorator Pattern

 Basic Structure
 Concrete Components
 The object to add dynamic behaviors to
Decorator Pattern

 Basic Structure
 Abstract Decorator
 Enforces the contract of the Abstract Component
Class
 Allows for more decorator specific behaviors to be
defined
Decorator Pattern

 Basic Structure
 Concrete Decorator
 Implements the specific behavior
Decorator Pattern

 Real World Examples


 java.io
 Abstract Component Class
 InputStream
 Concrete Components
 FileInputStream, StringBufferInputStream…
 Abstract Decorator
 FilterInputStream
 Concrete Decorator
 BufferedInputStream, DataInputStream…
Decorator Pattern

 Example problem and Solution


Decorator Pattern

 Cons
 Can Cause “Class Explosion”
 Each Decorator is a class
 Code can be hard to find/debug
Strategy Pattern

 Or…How do I apply algorithms to data


dynamically?
Strategy Pattern – High Level

 Category : Behavioral
 Allows algorithms vary independently
from clients that use them
Strategy Pattern

 OO Principals
 Encapsulate what varies
 Favor composition over inheritance

 Program to interfaces

 Strive for loosely coupled designs between


objects
 Open/closed principle
Strategy Pattern

 Basic structure
 Context
 Strategy Interface

 Concrete Strategies
Strategy Pattern
 Context
 Is Configured with a
Concrete Strategy
 Initiates the work of
the Strategy via the
Strategy Interface
 May provide and
interface for the
Strategy to access
it Data
Strategy Pattern
 Strategy Interface
 Defines a way for
the Context to
interact with each
algorithm
Strategy Pattern
 Concrete Strategy
 Encapsulates the
algorithm
 Performs the actual
work
Strategy Pattern

 Real World Examples


 Custom Sort options for lists
Strategy Pattern

 Example problem and Solution


Strategy Pattern

 Q&A
Strategy Pattern

 DONE

You might also like