What is so special about
At any given moment, somewhere in the world,
someone struggles with the same software design
problems you have. But better yet, someone has
already solved your software design problems.
Tried-and-true, road-tested, successfully used by
developers to create functional, elegant, reusable, and
The Duck Game
Someone wants ﬂy()
Let’s Design it together Code reuse?
Waiting for a design
pattern to save the
day? No :P
Design Priciple 1
Identify the aspects of
your application that
vary and separate them
from what stays the
The one constant in
Adding a spice of ﬂexibility
Design Priciple 2
Program to an interface,
not an implementation
OO Design Principles
Indentify the aspects of your application that vary and
separate them from what stays the same.
Program to an interface, not an implementation.
Favor composition over inheritance
Deﬁnes a family of algorithms, encapsulates each one,
and make them interchangeable.
Strategy lets the algorithm vary independently from
clients that use it.
How do I use design patterns?
If design patterns are so great, why can’t somebody
build a library of them so I don’t have to?
Aren’t libraries and frameworks also design patterns?
Knowing OO basics is enough? What makes me a
good OO designer?
The most heavily used pattern in the JDK
Publisher + subscriber = Observer Pattern
Deﬁnes a one-to-many relationship.
Deﬁnition: deﬁnes one-to-many dependency between
objects so that when one object changes state, all of
its dependencies are notiﬁed and updated
Concern the ways and means of object instantiation
Deal with the mutual composition of classes or
Analyze the ways in which classes or objects interact
and distribute responsibilities among them.
One of a kind objects.
Private constructors? Useful?
Ensures a class has only one instance, and provide a
global point of access to it.
Encapsulates a request as an object, thereby letting
you parameterize other objects with different requests,
queue or log requests, and support undoable
Adapter and Facade
Adapter pattern: converts the interface of a class into
another interface the clients expect. Adapters lets
classes work together that couldn’t otherwise because
of incompatible interfaces.
Facade pattern: provides a uniﬁed interface to a set of
interfaces in a subsystem. Facade deﬁnes a higher-
level interface that makes the subsystem easier to use.
OO Design Principle
Least Knowledge: talk only to your immediate friends.
How java violates it?
How facade use it?
Provides a way to access the elements of an aggregate
object (collection) sequentially without exposing its
OO Design Principle
Single Responsibility: A class should have only one
reason to change.
Cohesion: a term measures how closely a class or a
module supports a single purpose or responsibility.
High Cohesion -> if a class is designed around a set of
Allows you to compose objects into tree structures to
represent part-whole hierarchies, Composite lets clients
treat individual objects and compositions of objects
A little known fact: the strategy and state patterns were
twins separated at birth.
Allows an object to alter its behavior when its internal
state changes. The object will appear to change its
Deﬁnition: Provides a surrogate or placeholder for
another object to control access to it.
Patterns are often used together and combined within
the same design solution.
A compound pattern combines two or more patterns
into a solution that solves a recurring or general
A particular slide catching your eye?
Clipping is a handy way to collect important slides you want to go back to later.