Design Patterns
Design Patterns
Contents
• Overview
• Definitions
• Benefits & Drawbacks
• Kinds of Patterns
• Design Pattern Catalog
• Design Patterns Descriptions
• Summary
3
Objectives
• At the end of this presentation, participants
will be able to:
• Define Design Patterns
• Classify Design Patterns
• Identify Common Design Patterns
• Describe Each of the Design Patterns
4
Overview
In 1994, Design Patterns: Elements of Reusable Object-
Oriented Software by Erich Gamma, Richard Helm, Ralph
Johnson and John Vlissides explained the usefulness of
patterns and resulted in the widespread popularity of design
patterns.
Definitions
• A design pattern is a documented best practice or core of
a solution that has been applied successfully in multiple
environments to solve a problem that recurs in a specific
set of situations
• Design patterns are recurring solutions to software design
problems you find again and again in real-world
application development
• Design patterns represent solutions to problems that arise
when developing software within a particular context (i.e.,
Pattern = problem/solution pair in context)
• Design patterns are standard solutions to common
problems in software design
6
Drawbacks:
• Patterns do not lead to direct code reuse
• Patterns are deceptively simple
• Teams may suffer from pattern overload
• Patterns are validated by experience and discussion rather than by
automated testing
7
Factory Method
Definition
• Defines an interface for creating an object, but let subclasses decide
which class to instantiate. Factory Method lets a class defer
instantiation to subclasses
Problem & Context
• If an object needs to know the selection criteria to instantiate an
appropriate class, this results in a high degree of coupling. Whenever
the selection criteria change, every object that uses the class must be
changed correspondingly
Solution
• Encapsulate the functionality required to select and instantiate the
appropriate class. One way to do this is to create an abstract class or
an interface that declares the factory method. Different subclasses
can then implement the method in its entirety
10
Builder
Definition
• Separates the construction of a complex object from its representation
so that the same construction process can create different
representations
Problem & Context
• Object construction details are kept within the object as part of its
constructor. This may not be effective when the object being created is
complex and the object creation process produces different
representations of the object. The object can become bulky
(construction bloat) and less modular
Solution
• Move the construction logic out of the object class to separate classes
referred to as builder classes. A dedicated object referred to as a
Director, is responsible for invoking different builder methods required
for the construction of the final object. Different client objects can make
use of the Director object to create the required object
11
Prototype
Definition
• Specifies the kind of objects to create using a prototypical
instance and creates new objects by copying this
prototype
Problem & Context
• When clients need to create a set of objects that are cost
prohibitive and alike or differ only in terms of their state,
create one object upfront and designate it as a prototype
object or simply make a copy of the prototype object
Solution
• Provide a way for clients to create a copy of the prototype
object. By default, all Java objects inherit the built in
clone() method that creates a clone of the original object
12
Singleton
Definition
• Ensures a class has only one instance and provide a global point of
access to it
Problem & Context
• Sometimes there may be a need to have one and only one instance
of a given class during the lifetime of an application. This may be due
to necessity or, more often, because only a single instance of the
class is sufficient
Solution
• Create a class with a method that creates a new instance of the
object if one does not exist. If one does exist, it returns a reference to
the object that already exists. To make sure that the object cannot be
instantiated any other way, the constructor is made either private or
protected
13
Singleton (Diagram)
14
Adapter
Definition
• Converts the interface of a class into another interface clients expect.
Adapter lets classes work together that couldn't otherwise because of
incompatible interfaces
Problem & Context
• Sometimes an existing class may provide the functionality required by
a client, but its interface may not be what the client expects. In such
cases, the existing interface needs to be converted into an interface
that the client expects, preserving the reusability of the existing class
Solution
• Define a wrapper class around the object with the incompatible
interface. The adapter provides the required interface expected by the
client. The implementation of the adapter interface converts client
requests into calls to the adaptee class interface
18
Decorator
Definition
• Attaches additional responsibilities to an object dynamically.
Decorators provide a flexible alternative to subclassing for extending
functionality
Problem & Context
• This pattern allows new/additional behavior to be added to an existing
method of an object dynamically. Since classes cannot be created at
runtime and it is typically not possible to predict what extensions will be
needed at design time, a new class would have to be made for every
possible combination. By contrast, decorators are objects, created at
runtime, and can be combined on a per-use basis
Solution
• Pass the original object as a parameter to the constructor of the
decorator, with the decorator implementing the new functionality. The
interface of the original object needs to be maintained by the decorator
25
Command
Definition
• Encapsulates a request as an object, thereby letting you parameterize
clients with different requests, queue or log requests, and support
undoable operations
Problem & Context
• Used when there is a proliferation of similar methods and the interface
to implement an object becomes unwieldy – too many public methods
for other objects to call, an interface that is unworkable and always
changing
Solution
• Create an abstraction for the processing in response to client requests
by declaring a common interface to be implemented by different
concrete implementers referred to as Command objects. A given
Command object does not contain the actual implementation of the
functionality. Command objects make use of Receiver objects in
offering this functionality
27
Iterator
Definition
• Provides a way to access the elements of an aggregate object
sequentially without exposing its underlying representation
Problem & Context
• The purpose of an iterator is to process every element of a container
while isolating the user from the internal structure of the container. The
container allows the user to treat it as if it were a simple sequence or
list while storing elements in any manner it wishes. Iterators can
provide a consistent way to iterate on data structures of all kinds,
making the code more readable, reusable, and less sensitive to a
change in the data structure
Solution
• In Java, the java.util.Iterator interface allows you to iterate container
classes. Each Iterator provides a next() and hasNext() method, and
may optionally support a remove() method. Iterators are created by the
method iterator() provided by the corresponding container class
28
Iterator (Diagram)
38
Key Points
• Design Patterns are recurring solutions to software design
problems within a particular context
References
• Design Patterns: Elements of Reusable Object-Oriented Software
By: Erich Gamma, et al.
• Software Architecture Design Patterns in Java
By: Partha Kutchana
• The Design Patterns Java Companion
By: James W. Cooper
• http://www.dofactory.com/Patterns/Patterns.aspx
• http://www.cmcrossroads.com/bradapp/docs/patterns-intro.html
• http://en.wikipedia.org/wiki/Design_pattern_(computer_science)