Вы находитесь на странице: 1из 71

Module 3:

UML In Action - Design Patterns

Overview

Books Design Patterns Basics Creational Design Patterns Structural Design Patterns Behavioral Design Patterns

Book

Design Patterns : Elements of Reusable Object-Oriented Software (1995) (The-Gang-of-Four Book) The-Gang-of-Four (GoF) - Gamma, Helm, Johnson , Vlissides

Life Challenges
Software Design!!!

Design Patterns
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

This was in describing patterns in buildings and towns. In SE, design patterns are in terms of objects and interfaces, not walls and doors.
The manner in which a collection of interacting objects collaborate to accomplish a specific task or provide some specific functionality.

Architecture vs. Design Patterns


Architecture

High-level framework for structuring an application


client-server based on remote procedure calls abstraction layering distributed object-oriented system based on CORBA

Defines the system in terms of computational components & their interactions Lower level than architectures (Sometimes, called micro-architecture) Reusable collaborations that solve subproblems within an application

Design Patterns

how can I decouple subsystem X from subsystem Y?

Why Design Patterns?


Design patterns support object-oriented reuse at a high level of abstraction Design patterns provide a framework that guides and constrains object-oriented implementation

4 Essential Elements of Design Patterns

Name: identifies a pattern Problem: describes when to apply the pattern in terms of the problem and context Solution: describes elements that make up the design, their relationships, responsibilities, and collaborations Consequences: results and trade-offs of applying the pattern

Design Patterns in MVC

Model: the application object View: its screen presentation Controller : defines the way the user interface reacts to user input MVC decouples views and models by establishing a subscribe/notify protocol between them

How to Describe Design Patterns more fully


This is critical because the information has to be conveyed to peer developers in order for them to be able to evaluate, select and utilize patterns.

A format for design patterns


Pattern Name and Classification Intent Also Known As Motivation Applicability Structure Participants Collaborations Consequences Implementation Sample Code Known Uses Related Patterns
9

Organizing Design Patterns

By Purpose (reflects what a pattern does): Creational Patterns Structural Patterns Behavioral Patterns By Scope: specifies whether the pattern applies primarily to classes or to

objects.

10

Design Patterns Space


Purpose Structural
Adapter Adapter Bridge Composite Decorator Faade Flyweight Proxy

Creational
Scope Class Object
Factory Method Abstract Factory Builder Prototype Singleton

Behavioral
Interpreter Template Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Visitor
11

How Design Patterns Solve Design Problems (1)

Finding Appropriate Objects

encapsulation, granularity, dependency, flexibility, performance, evolution, reusability,

Determining Object Granularity Specifying Object Interfaces


Signature Interface Type Dynamic Binding Polymorphism

Specifying Object Implementations

12

How Design Patterns Solve Design Problems (2)

Specifying Object Implementations

13

Principles of Object-Oriented Design


Programming to an Interface, not an Implementation Design for Reuse


White-box reuse Black-box reuse Delegation

Favor object composition over class inheritance

Inheritance versus Parameterized Types

e.g., Templates in C++


14

Software Design
Big problems in practice Big opportunities in research

Have Fun!!

Why modularization?

Managerial Changeability Comprehensibility

What is a Modularization?

Making design decisions before the work on independent modules can begin. Quite different decisions are included for each alternative In all cases, the intention is to describe all "system level" decisions (i.e. decisions which affect more than one module).

A Canonical Topic

On the Criteria To Be Used in Decomposing Systems into Modules

David Parnas, 1972

A Canonical Example
Key Word in Context [Parnas72]
The KWIC index system accepts an ordered set of lines, each line is an ordered set of words, and each word is an ordered set of characters. Any line may be "circularly shifted" by repeatedly removing the first word and appending it at the end of the line. The KWIC index system outputs a listing of all circular shifts of all lines in alphabetical order.

A Canonical Example
Key Word in Context [Parnas72]
1. 2. 3. 4.

Input Circular Shift Alphabetizing Output

Software Architecture Sense and Sensibility Crouching Tiger Hidden Dragon Architecture Software and Sensibility Sense Sensibility Sense and Tiger Hidden Dragon Crouching Hidden Dragon Crouching Tiger Dragon Crouching Tiger Hidden

Making your First Decision

How should such a system be designed? Modularized? What are the factors to consider?

Thinking in the perspective of

Software Design! What does it mean? 1. Elements

2. Relations

Describe the Design:

1. Plain English

2. Elements and Relations

Main Program/Subroutine

Module1: Input
This module reads the data lines from the input medium and stores them in core for processing by the remaining modules. The characters are packed four to a word, and an otherwise unused character is used to indicate the end of a word. An index is kept to show the starting address of each line.

Build KWIC Architecture


Input

Characters

Line Index

Input Medium

System I/O Data Repr in Memory Direct Memory Access

Module 2: Circular Shift


This module is called after the input module has completed its work. It prepares an index which gives the address of the first character of each circular shift, and the original index of the line in the array made up by module 1. It leaves its output in core with words in pairs (original line number, starting address)

Build KWIC Architecture


Input Circular Shifter

Characters

Line Index

Shifts Index

Input Medium

Module 3: Alphabetizing
This module takes as input the arrays produced by modules 1 and 2. It produces an array in the same format as that produced by module 2. In this case, however, the circular shifts are listed in another order (alphabetically).

Build KWIC Architecture

Input

Circular Shifter

Alphabetizer

Characters

Line Index

Shifts Index

Sorted Shifts Index

Input Medium

Module 4: Output
Using the arrays produced by module 3 and module 1, this module produces a nicely formatted output listing all of the circular shifts. In a sophisticated system the actual start of each line will be marked, pointers to further information may be inserted, and the start of the circular shift may actually not be the first word in the line, etc.

Build KWIC Architecture

Input

Circular Shifter

Alphabetizer

Output

Characters

Line Index

Shifts Index

Sorted Shifts Index

Input Medium

Output Medium

Module 5: Master Control


This module does little more than control the sequencing among the other four modules. It may also handle error messages, space allocation, etc .

Build KWIC Architecture

Is this a good design?

What if:

Memory Size change? Input Size change? Input Format change? Alphabetizing Policy change?

Consider the Following Changes:


1. Input Format
Module 1

2. The decision to have all lines stored in core. For

large jobs it may prove inconvenient or impractical to keep all of the lines in core at any one time.

All Modules!!!

3. The decision to pack the characters four to a word. In cases where we are working with small amounts of data it may prove undesirable to pack the characters; time will be saved by a character per word layout. In other cases we may pack, but in different formats.
All Modules!!!

4. The decision to make an index for the circular shifts

rather than actually store them as such. Again, for a small index or a large core, writing them out may be the preferable approach.
Module 2, 3

5. The decision to alphabetize the list once, rather than either (a) search for each item when needed, or (b) partially alphabetize as is done in Hoare's FIND [2]. In a number of circumstances it would be advantageous to distribute the computation involved in alphabetization over the time required to produce the index. .
Module 3

In Summary
A Canonical Example Describe its Architecture Evaluate its Changeability

Data Abstraction
Elements

Objects Interfaces I/O Medium Method Invocation System I/O

Relations

Parnass Modularization 2
Module Module Module Module Module Module

1: 2: 3: 4: 5: 6:

Line Storage Input Circular Shift Alphabetizer Output Master Control

Our 3-Step Exercise

1. 2. 3.

Read module description Identify architecture elements and relations Analyze changeability

Module 1: Line Storage


This module consists of a number of functions or subroutines which provide the means by which the user of the module may call on it. The function call CHAR(r,w,c) will have as value an integer representing the cth character in the rth line, wth word. A call such as SETCHAR(rpv,c,d) will cause the cth character in the wth word of the rth line to be the character represented by d (i.e. CHAR(r,w,c) = d). WORDS(r) returns as value the number of words in line r.

Build OO Architecture
Module 1: Line Storage
Public Interface addWord getWord addLine getLine Modules (Objects) 1.LineStorage

Module 2: Input
This module reads the original lines from the input media and calls the line storage module to have them stored internally.

Build OO Architecture
Module 2: Input
2. Input System I/O

Method Invocation addWord getWord addLine Input Medium getLine

Public Interface

1.LineStorage

I/O Medium Modules (Objects)

Module 3: Circular Shifter


The principal functions provided by this module are analogs of functions provided in module 1. The module creates the impression that we have created a line holder containing not all of the lines but all of the circular shifts of the lines. Thus the function call CSCHAR(I,w,c) provides the value representing the cth character in the wth word of the Ith circular shift. It is specified that (1) if i < j then the shifts of line i precede the shifts of line j, and (2) for each line the first shift is the original line, the second shift is obtained by making a one-word rotation to the first shift, etc. A function CSSETUP is provided which must be called before the other functions have their specified values.

Build OO Architecture
Module 3: Circular Shifter
2. Input

addWord

getWord

Input Medium

addLine

1. LineStorage

getLine

getLine

3. Circular Shifter

setup

Module 4: Alphabetizer
This module consists principally of two functions. One, ALPH, must be called before the other will have a defined value. The second, ITH, will serve as an index. ITH(i) will give the index of the circular shift which comes ith in the alphabetical ordering.

Build OO Architecture
Module 4: Alphabetizer
2. Input

addWord

getWord

Input Medium

addLine

1. LineStorage

getLine

getLine

getLine

Alpha

setup

3. Circular Shifter

4. Alphabetizer

Module 5: Output
This module will give the desired printing of set of lines or circular shifts.

Build OO Architecture
Module 5: Output
2. Input 5. Output

addWord

getWord

Input Medium

addLine

1. LineStorage

getLine

getLine

getLine

Alpha

setup

3. Circular Shifter

4.Alphabetizer

Output Medium

Module 6: Master Control


Similar in function to the modularization above (The first modularization)

Build OO Architecture
Module 6: Master Control

Is this a good design?

What if:

Memory Size change? Input Size change? Input Format change? Alphabetizing Policy change?

Consider the Following Changes:


1. Input Format change
Input module

2. The decision to have all lines stored in

core. For large jobs it may prove inconvenient or impractical to keep all of the lines in core at any one time.
LineStorage Module

3. The decision to pack the characters four to a word. In cases where we are working with small amounts of data it may prove undesirable to pack the characters; time will be saved by a character per word layout. In other cases we may pack, but in different formats.
LineStorage Module

4. The decision to make an index for the

circular shifts rather that actually store them as such. Again, for a small index or a large core, writing them out may be the preferable approach.
Circular Shift Module

Changeability Analysis Summary

Changes
1. Input Format

Affected Modules
Input

2. Not to store all lines in core

Line Storage Circular Shift

3. Not to pack characters Line Storage

4. Store full shifted lines

5. Different Alphabetizing Alphabetizer

Changeability Analysis Summary

A much better choice than the main program/subroutine style under these changes!!

High-level Design Detailed Design

Architecture

UML

OO Architecture and UML

Class Diagram: System Statics Sequence Diagram: System Dynamics

OO Architecture UML Class Diagram

Class diagram

Describes how modules from the original architectural solution map onto classes. Depicts all classes from the system we are implementing. For each class it depicts its public interface, i.e., public methods that we may invoke to manipulate instances of that class. For each class it depicts its instance variables. Shows how classes relate to each other. Class relations are represented with so-called associations, their type and attributes.

OO Architecture UML Class Diagram

Each module one class. Public interfacethe bottom sub-box, as a list of method declarations.

OO Architecture UML Class Diagram

Class Relations

A directed association: the originating class has as an instance variable an object of the associated class. For example, the Alphabetizer class has an association with the CircularShift class.

UML Sequence Diagram: System Dynamics

Describe how group of objects collaborate to execute a certain task in a running program. Describes the time sequence of method invocations on the run-time:

Objects are shown as boxes at the top of the diagram. Time is represented with the so-called object's lifeline. Method invocation is symbolized through a horizontal line connecting two vertical object's lifelines. Conditions for method invocation and iterative method invocations may be symbolized with special signs.

UML Sequence Diagram: System Dynamics

Design for Change


Creating an object by specifying a class explicitly Dependence on specific operations Dependence on hardware and software platform Dependence on object representations or implementations Algorithmic dependencies Tight coupling Extending functionality by subclassing Inability to alter classes conveniently

71

Вам также может понравиться