Introduction to Design Patterns
“Reusable Object Oriented
Software Elements”
What Is A Pattern?
• "Each pattern describes a problem which occurs over and
over again in our environment, and then describes the core of
the solution to that problem, in such a way that you can use
this solution a million times over, without ever doing it the
same way twice." -- Christopher Alexander 1977
Design Patterns
• “Each pattern is a three-part rule, which expresses a
relation between a certain context, a problem and a
solution.”
• Hence, the common definition of a pattern: “A solution to
a problem in a context.”
Why Patterns?
– "Designing object-oriented software is hard and designing
reusable object-oriented software is even harder." - Erich Gamma
– Experienced designers reuse solutions that have worked in the
past
– Well-structured object-oriented systems have recurring patterns of
classes and objects
– Knowledge of the patterns that have worked in the past allows a
designer to be more productive and the resulting designs to be
more flexible and reusable
Common Uses
• Common vocabulary to all developers.
• Best Practices.
Properties
Patterns do
• Provide common vocabulary
• Provide “shorthand” for effectively communicating
complex principles
• Help document software architecture
• Capture essential parts of a design in compact form
• Show more than one solution
• Describe software abstractions
Patterns do not
• Provide exact solution
• Solve all design problems
• Only apply for object-oriented design
Becoming a Software Design Master
• First learn the rules
– E.g., the algorithms, data structures and languages of software.
• Then learn the principles
– E.g., structured programming, modular programming, object-
oriented programming, generic programming, etc.
• However, to truly master software design, one must study the
designs of other masters
• These designs contain patterns must be understood,
memorized, and applied repeatedly
• There are hundreds of these patterns
Software Patterns History
– 1987 - Cunningham and Beck used Alexander’s ideas to develop
a small pattern language for Smalltalk
– 1990 - The Gang of Four (Gamma, Helm, Johnson and Vlissides)
begin work compiling a catalog of design patterns
– 1991 - Bruce Anderson gives first Patterns Workshop at
OOPSLA
– 1993 - Kent Beck and Grady Booch sponsor the first meeting of
what is now known as the Hillside Group
–
1994 - First Pattern Languages of Programs (PLoP) conference
1995 - The Gang of Four (GoF) publish the Design Patterns book
Types Of Software Patterns
– Analysis
– Design
– Organizational
– Process
– Project Planning
– Configuration Management
GoF Classification Of Design Patterns
– Purpose - what a pattern does
• Creational Patterns
› Concern the process of object creation
• Structural Patterns
› Deal with the composition of classes and objects
• Behavioral Patterns
› Deal with the interaction of classes and objects
– Scope - what the pattern applies to
• Class Patterns
› Focus on the relationships between classes and their subclasses
› Involve inheritance reuse
• Object Patterns
› Focus on the relationships between objects
› Involve composition reuse
Classification of design patterns
Scope: domain over which a pattern applies
Purpose: reflects what a pattern does
GoF Pattern Template
– Pattern Name and Classification
• A good , concise name for the pattern and the pattern's type
– Intent
•Short statement about what the pattern does
– Also Known As
• Other names for the pattern
– Motivation
•A scenario that illustrates where the pattern would be useful
– Applicability
• Situations where the pattern can be used
GoF Pattern Template (Continued)
– Structure
• A graphical representation of the pattern
– Participants
• The classes and objects participating in the pattern
– Collaborations
• How to do the participants interact to carry out their responsibilities
– Consequences
• What are the pros and cons of using the pattern?
– Implementation
•Hints and techniques for implementing the pattern
GoF Pattern Template (Continued)
– Sample Code
• Code fragments for a sample implementation
– Known Uses
• Examples of the pattern in real systems
– Related Patterns
• Other patterns that are closely related to the pattern
Benefits Of Design Patterns
– Capture expertise and make it accessible to non-experts in a
standard form
– Facilitate communication among developers by providing a
common language
– Make it easier to reuse successful designs and avoid alternatives
that diminish reusability
– Facilitate design modifications
– Improve design documentation
– Improve design understandability
OBSERVER
Intent:
Define one-to-many dependency between objects so that
when one object changes state, all its dependents are notified
and updated automatically.
Also Known as :
Dependents, Publish-Subscribe, Model-View
OBSERVER
PROBLEM CONTEXT
• An object (called as a subject) is the source of event
• One or more objects (called as observers) want to know
when the event occurs
SOLUTION
• Define a subject class that provides a method to attach
observers to it
• Subject maintains the set of observers attached to it
• An Observer interface defines a method as a means to
receive notification from subject (the event source)
… Observer
• All concrete observers implement this interface
type. They realize the interface method to define
action to be taken on event generation
• On generation of event, the subject notifies all the
observers attached to it through their common
interface method
…Observer
CLASS DIAGRAM
<<interface>>
Subject Observer
attach(Observer O)
notify()
Concrete
Observer1
notify()
Concrete
Observer2
Implements action to be taken on
receiving notification from subject
notify()
Events on JComponents are received and
processed using Observer
<<interface>>
JButton ActionListener
addActionListener() actionPerformed()
Concrete
Observer1(A class
implementing
ActionListener)
actionPerformed()
Concrete
Observer2
Implements action to be taken on actionPerformed()
receiving notification from Jbutton when it
is clicked
Observer Design Pattern
Name in Design Pattern Actual Name
Subject – JButton
Observer – ActionListener
ContreteObserver – class that implements
ActionListener i/f type
attach() – add ActionListener
notify – actionPerformend
Consequence:
• Abstract coupling between Subject and
Observer.
• Support for broadcast communication