1 references: xiaoping jia, object-oriented software development using java;douglas c.schmidt,...

21
1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

Upload: joy-ford

Post on 18-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

1

References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

Page 2: 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)

Page 3: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

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

Page 4: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

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

Page 5: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

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

Page 6: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

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

Page 7: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

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)

Page 8: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

8

Singleton Pattern• Structure

Singleton

static getInstance()

operation()

getData()

static Singleton theInstance

data

return theInstance

Page 9: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

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.

Page 10: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

10

Singleton Patternpublic class Singleton {

public static Singleton getInstance() {

return theInstance;

}

private Singleton() {

//initialize instance fields

}

//….

private static Singleton theInstance = new Singleton();

}

Page 11: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

11

Strategy Pattern

• Category : behavioral

• Intent: define a family of algorithms, encapsulate each one and make them interchangeable

Page 12: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

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

Page 13: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

13

Structure of Strategy Pattern

ContextcontextInterface()

StrategyalgorithmInterface()

STRATEGY

Concrete Strategy AalgorithmInterface()

Concrete Strategy BalgorithmInterface()

Concrete Strategy CalgorithmInterface()

Note: arrowheads should be unfilled

Page 14: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

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

Page 15: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

15

A tutorial presented by Steve Metsker and William WakeAt OOPSLA 2001

Page 16: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

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

Page 17: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

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

Page 18: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

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

Page 19: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

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

Page 20: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

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?

Page 21: 1 References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++

21