Introduction To Design Patterns "Reusable Object Oriented Software Elements"
Introduction To Design Patterns "Reusable Object Oriented Software Elements"
• 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.
– 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
– Sample Code
– Known Uses
– Related Patterns
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
<<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
Subject – JButton
Observer – ActionListener
notify – actionPerformend
Consequence:
• Abstract coupling between Subject and
Observer.