1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt,...
Transcript of 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt,...
1
References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++
2
Design Patterns
• Generic (reusable) solutions to a recurring problem
• Evolved from Christopher Alexander’s patterns for architecture.– “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.” – Alexander et. al., A Pattern Language (Oxford U.Press,1977)
3
Design Patterns–a ‘Hot Topic’
• OOPSLA Workshops – 17%– Patterns for Software Architecture– Killer Examples for Design Patterns &
Objects First– Patterns in Distributed Real Time &
Embedded Systems– Patterns for Customer Interaction &
Expectation Management– Software Development Process Patterns
4
Design Patterns–a ‘Hot Topic’• OOPSLA Tutorials – 20%
– Pattern-oriented Software Architectures for Networked and Concurrent Applications
– Patterns of Enterprise Application Architecture– Patterns at Work– Patterns for Writing Effective Use Cases– Dungeons and Patterns!– Patterns and Application Experiences for Real-time Object
Request Brokers– Refactoring to Patterns– Object-oriented Reengineering: Patterns & Techniques– Patterns for EJB Development– How to Use Design Patterns in Java and .NET
5
Design Patterns
• Categories of Software Design Patterns– Creational : deal with process of creation– Structural : deal with static composition
and structure of classes and objects– Behavioral : deal with dynamic interaction
among classes and objects
6
Design Patterns
• Each pattern is described as follows:
Pattern name
Category : creational, structural or behavioral
Intent : short description of problem addressed
AKA: other names for the pattern
Applicability : where pattern can be applied
Structure : diagram that describes participants in the pattern & relationships among them
Participants : list of classes and/or objects that participate in the pattern
7
Singleton Pattern
• Category: creational
• Intent: ensure that a class has only one instance and provide global point of access to it
• Applicability: use where there must be exactly one instance of a class and it must be accessible to clients from a well-known access point (e.g. database)
8
Singleton Pattern• Structure
Singleton
static getInstance()
operation()
getData()
static Singleton theInstance
data
return theInstance
9
Singleton Pattern
• Participants : only one
• Singleton declares the unique instance of the class as a static variable and defines a static method getInstance() for clients to access the unique instance.
10
Singleton Patternpublic class Singleton {
public static Singleton getInstance() {
return theInstance;
}
private Singleton() {
//initialize instance fields
}
//….
private static Singleton theInstance = new Singleton();
}
11
Strategy Pattern
• Category : behavioral
• Intent: define a family of algorithms, encapsulate each one and make them interchangeable
12
Strategy Pattern• Applicability: use when
– many related classes differ only in behavior• plotting different functions
– different variations of algorithm are needed• sorts
– an algorithm uses data that clients should not know about
• LayoutManager in the AWT
– a class defines many behaviors which appear as multiple conditional statements in its methods
13
Structure of Strategy Pattern
ContextcontextInterface()
StrategyalgorithmInterface()
STRATEGY
Concrete Strategy AalgorithmInterface()
Concrete Strategy BalgorithmInterface()
Concrete Strategy CalgorithmInterface()
Note: arrowheads should be unfilled
14
Using the Strategy Pattern
quicksortPivot StrategygetPivot()
SelectFirst
Random Medianof
Three
pivotStrategy.getPivot(array,lo,hi)
Strategy pattern resolves how to extend policies for selecting a pivot value without modifying main quicksort algorithm
Concrete Strategies
15
A tutorial presented by Steve Metsker and William WakeAt OOPSLA 2001
16
Why Design Patterns?
• Patterns record previous successes in a reusable form
• Design patterns are well-worn solutions to problems at about a class level
17
How the Game Works
• Each table has a pattern master who has a map of a dungeon
• You must adventure through the dungeon to reclaim pearls of wisdom from a dragon
• Your primary helper is an Ahobbit– With capricious magical abilities
18
Ahobbits• Your Ahobbit has a magical ring that
– Cannot be removed– You can activate by getting him/her to say
“Aha”
• Ahobbits are enlightened only when they understand a design pattern
• Ahobbits learn from real-world examples in the dungeon
19
Adventuring• Find a room in the dungeon
• When you enter– Examine the device or contents therein– Determine which pattern it shows– Use the device to explain to your Ahobbit
the intent of the pattern (2 guesses)
• When the Ahobbit says “Aha,” something magical will occur
20
Beginning• We shall begin as one huge
exploration party
• Before breaking into one party per table or group
• You stand before a door that leads to the dungeon
• What do you do?
21