Design Patterns
A good class is a lot like an iceberg: seven-
eights is under water, and you can see only the
one-eight that’s above the surface.
Tempus
McConnell
Module Topics (Cont’d)
Introduction to Design Patterns
Creational Patterns
Structural Patterns
Behavioral Patterns
Behavioral Patterns Discussion
The main concern of behavioral patterns is the communications and the
assignment of responsibilities between objects.
Behavioral patterns describe patterns of classes/objects and
communication among them.
Behavioral class patterns use inheritance to distribute behavior
between classes.
Behavioral object patterns use object composition rather than
inheritance.
Other behavioral object patterns are concerned with encapsulating
behavior in an object and delegating requests to it.
Behavioral Patterns Discussion
Behavioral class patterns:
Template Method
Interpreter
Behavioral object patterns:
Mediator
Chain of Responsibility
Observer
Encapsulating behavior patterns:
Strategy
Command
State
Visitor
Iterator
Behavioral Design Patterns
Behavioral Patterns
Chain of Responsibility Observer
Command State
Interpreter Strategy
Iterator Template Method
Mediator Visitor
Memento
Behavioral class patterns: use inheritance to
distribute behavior between classes.
Template Method
Template Method
Template Method
Template Method
Template Method
Template Method
Template Method
Template Method
Template Method
Template Method
Template Method
Encapsulating behavioral object patterns:
encapsulating behavior in an object and delegating
requests to it.
Iterator
Strategy
Iterator Pattern
Intent: Provide a way to access the elements of an aggregate
object sequentially without exposing its underlying
representation.
Also Known As: Cursor
Iterator
Iterator
What is <T>?
• It tells Java that this interface works with a generic type T.
• The actual type T will be specified later when someone uses or
implements the interface.
• Why uses generics?
• To write type-safe and reusable code
• You don’t have to cast objects manually
• Prevents runtime ClassCastException
Generic Type T example
• In each case, T
becomes:
• String
• Integer
• User (custom class)
With and without Generics:
Iterator
Iterator
Iterator
Client
represents the
usage of the
pattern; that’s
the main
method in this
example
Iterator
Called
Collection
of
Aggregate
Iterator
Iterator
Iterator
Iterator Pattern
Another Example
Iterator (Cont’d)
Structure
Strategy Pattern
Factory Method
Abstract Factory
Strategy
Bridge
Strategy Pattern
• Ternary operator
• Equivalent to:
Strategy Pattern
Strategy Pattern
Strategy Pattern
Strategy Pattern
Strategy Pattern
Strategy Pattern
Strategy Pattern
Strategy Pattern
Strategy Pattern
Strategy Pattern
Client
Strategy Pattern
Strategy Pattern
Strategy Pattern
Strategy Pattern
Bridge and Strategy Design Patterns
Abstract Factory always
deal with the creation of
objects growing in two
independent dimensions
Strategy Pattern
Another Example
Behavioral object patterns
Mediator
Observer
Mediator Pattern
Mediator Pattern
Mediator Pattern
Mediator Pattern
Mediator Pattern
Mediator Pattern
Mediator Pattern
Mediator Pattern
Mediator Pattern
Another Example
Observer Pattern
Watch Video!
Observer Pattern
Observer Pattern
NotificationService
us usually implemented as
a singleton class
Observer Pattern
Observer Pattern
Observer Pattern
Observer Pattern
Observer Pattern
Observer Pattern
Observer Pattern
Observer Pattern
Observer Pattern
Observer Pattern
Another Example
Other Behavioral Patterns’ Intent
Chain of Responsibility: Avoid coupling the sender of a
request to its receiver by giving more than one object a chance
to handle the request. Chain the receiving objects and pass the
request along the chain until an object handles it.
Command: Encapsulate a request as an object, thereby letting
you parameterize clients with different requests, queue or log
requests, and support undoable operations.
Interpreter: Given a language, define a representation for its
grammar along with an interpreter that uses the representation
to interpret sentences in the language.
Other Behavioral Patterns’ Intent (Cont’d)
Mediator: Define an object that encapsulates how a set of
objects interact. Mediator promotes loose coupling by keeping
objects from referring to each other explicitly, and it lets you
vary their interaction independently.
Memento: Without violating encapsulation, capture and
externalize an object's internal state so that the object can be
restored to this state later.
Observer: Define a one-to-many dependency between objects
so that when one object changes state, all its dependents are
notified and updated automatically.
Other Behavioral Patterns’ Intent (Cont’d)
State: Allow an object to alter its behavior when its internal state changes.
The object will appear to change its class.
Strategy: Define a family of algorithms, encapsulate each one, and make
them interchangeable. Strategy lets the algorithm vary independently from
clients that use it.
Template Method: Define the skeleton of an algorithm in an operation,
deferring some steps to subclasses. Template Method lets subclasses redefine
certain steps of an algorithm without changing the algorithm's structure.
Visitor: Represent an operation to be performed on the elements of an
object structure. Visitor lets you define a new operation without changing the
classes of the elements on which it operates.
Thank you for
your attention.
Tempus