comp2110 software design lecture 13detailed design (1) detailed design contrasted with high level...

21
comp2110 Software Design lecture 13 Detailed Design (1) detailed design contrasted with high level design introducing the Observer Design Pattern information resources: o lecture 6 2003 and Tetris design lecture from 2002 http://cs.anu.edu.au/student/comp2110/archive-2002/lectures/lec03/lec03- notes.html o Braude Software Design o chapters 4 & 5 – background o chapter 6: Design Patterns o chapter 9: Observer No lectures next 2 weeks (weeks 8 & 9) gives time for your presentations in lab class times

Upload: merryl-flowers

Post on 29-Dec-2015

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

comp2110 Software Designlecture 13 Detailed Design (1)

detailed design contrasted with high level design introducing the Observer Design Pattern information resources:

o lecture 6 2003 and Tetris design lecture from 2002http://cs.anu.edu.au/student/comp2110/archive-2002/lectures/lec03/lec03-notes.html

o Braude Software Designo chapters 4 & 5 – backgroundo chapter 6: Design Patternso chapter 9: Observer

No lectures next 2 weeks (weeks 8 & 9)gives time for your presentations in lab class times

Page 2: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

Process Phase for this moduleRequirementsAnalysis

Design

Implementation

ArchitectureFramework Detailed Design

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 3: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

Recap from lecture 6: Design (1) Module structure

One way to describe the program's module structure is called a "module guide"o defines the name for each module and

the design responsibility for a moduleby stating the design decisions/ areas of design responsibility that will be found within it (not the functional responsibility)

o This is a decomposition of the solutiono decompose the solution into modules,

each module may consist of submoduleso the document should reflect a tree structure,

dividing the system into a small number of modulesand treating each module in the same wayuntil all modules are "quite small"

Note:o we also call this the system architecture or high-level designo there are other ways to choose and describe the architecture

Page 4: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

Design (1) Module structure/architecture

Architecture:● The system is event driven in a computational

loop that generates and drives a series of falling bricks in soft real time,using interrupt events to process user command keystrokes.

The system includes a reusable generic playing field for games with coloured tiles.

The system design requires a generic GUI library.

Page 5: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

Module relationships

uses relationships... between modules ...between classes

Page 6: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

Class relationships and interfaces

Page 7: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

Class relationships – control sequence

Page 8: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

Key Concept: Flexibility

We design flexibly, introducing parts,

because change and reuse are likely.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 9: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

Making a Method Re-usable

Specify completelyo Preconditions etc (see Braude section 1.2.2)

Avoid unnecessary coupling with the enclosing class1. Make static if feasible

2. Include parameterization1. i.e., make the method functional

2. But limit the number of parameters

Make the names expressive Understandability promotes re-usability

Explain the algorithm Re-users need to know how the algorithm works

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 10: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

Making a Class Re-usable

Describe the class completely

Make the class name and functionality

match a real world concept

1. Define a useful abstraction

o attain broad applicability

2. Reduce dependencies on other classes• Elevate dependencies in the hierarchy

alternatives

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 11: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

Example: design of a Command Line Calculator-requirements

1. CommandLineCalculator begins by asking the user how many accounts he wants to open. It then establishes the desired number, each with zero balance.

2. CommandLineCalculator asks the user which of these accounts he wants to deal with.

3. When the user has selected an account, CommandLineCalculator allows the user to add whole numbers of dollars to, or subtract them from the account for as long as he requires.

4. When the user is done with an account, he is permitted to quit, or to pick another account to process.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.(from chapter 1.4.1)

Page 12: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

A More Flexible Design for Calculator Application

CommandLineCalculator main()

executeAdditions()solicitNumberAccounts()getAnInputFromUser()

interactWithUser()

Existing Design New Design

Calculator solicitNumAccounts()

CalcDisplaydisplay()

CalcOperationexecute()

Add Multiply Divide

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 13: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

Two kinds of reuse

actual software (packages, classes) can be reused if designed wello but it is difficult to do design this well

existing ideas, concepts, general ways of putting components togethercan be adapted by the designer to new projectso Design Patterns are an excellent of describing

sets of well-known ideas for designers to reuse

Page 14: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

Design Patterns

Design Patterns have names:other designers can recognise what you are doing and playing variations on

Design Patterns have documented properties:where and how they work, what performance problems, what other restrictions

Design patterns can save lots of time learning by (bad) experience:pre-packaged (good) experiences

thinking in design patterns pushes us to think generally, more abstractly, more productively

Page 15: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

The point of studying design patterns

Enable you to re-use great design ideas Create a common vocabulary for talking about

design with designers, reviewers, programmers. Give you a higher-level perspective on design,

o allow you to talk and think at a higher level of abstraction (Forest vs. trees)

The solutions in the standard patterns embody some important design principleso in particular many of them produce code that is

much easier to modify than a more straightforward, simple-minded solution.

Page 16: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

Patterns: from their inventor, a bricks-and-mortar Architect

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

Page 17: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

Describing Software Design Patterns

Item Description

Name A unique name that identifies this pattern

Intent The purpose of this pattern

Problem The problem the pattern tries to solve

Solution How the pattern provides a solution to the problem

Participant and CollaboratorsThe entities (usually classes) involved in the pattern

ConsequencesThe consequences of using this pattern; discussion

of the forces at play

Implementation How to implement it

GoF Page number in the Gang of Four book (Gamma et al)

Page 18: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

Observer pattern

see also Braude 9.5

Problem:Changes to one object require changes to others that depend on it, but you don't know how many objects will need to be changed.

Examples: a networked file system a spreadsheet program with charts and graphs a set of different views (render, edit, window)

over a single complex structured text-like file (e.g. HTML)

Page 19: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

Observer (1)

Here is the "standard" pattern outline [GoF pp 293-303]

NameObserver IntentDefine a one-to-many dependency

between objects so that when one object changes state, all its dependents are notified and updated automatically.

Problem You need to notify a varying list of objects that an event has occurred.

Solution The observers delegate the responsibility for monitoring for an event to a central object: the subject (or Source- Braude)

Page 20: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

Observer (2)

Participants and Collaborators The subject knows its observers because they register with it. The subject must notify the observers when the event occurs. The observers are responsible both for registering with the subject and for getting the information they need from the subject when notified.

Consequences Subjects may tell observers about events they do not need to know about, if some observers are only interested in a subset of events. Extra communication is needed when the observers ask the subject for more information.

Page 21: Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern

Observer (3)

Implementation Have observers register with the subject. The subject is responsible for monitoring for (or generating) the interesting events. Observers need to keep a reference to the subject. The subject needs to keep a list of observers and to add observers or remove them from the list on request. When an interesting event occurs, the subject goes through its list and tells each observer to update itself.