Upload
byron-goodwin
View
220
Download
3
Tags:
Embed Size (px)
Citation preview
© Chinese University, CSE Dept. Software Engineering / 5 - 1
Software Engineering
Topic 5: Software Design
Your Name: _____________________
Computer Science & Engineering Department
The Chinese University of Hong Kong
© Chinese University, CSE Dept. Software Engineering / 5 - 2
Prelude
Software design should be based on s/w engineering principles– Rigor and formality
» Appropriate notations for design– Separation of concerns, Modularity, Abstraction
» Handle design complexity, make s/w understandable and reliable
– Anticipation of change, Incrementality» Produce evolvable s/w design
– Generality» Product family
© Chinese University, CSE Dept. Software Engineering / 5 - 3
Prelude
Software design should be based on s/w engineering principles– Rigor and formality
» Appropriate notations for design– Separation of concerns, Modularity, Abstraction
» Handle design complexity, make s/w understandable and reliable
– Anticipation of change, Incrementality» Produce evolvable s/w design
– Generality» Product family
© Chinese University, CSE Dept. Software Engineering / 5 - 4
Prelude
High quality design – modular structure– decomposition criteria
Information hiding: a module is characterized by the information it hides from other modules ___________– Information hiding + abstract data type
O-O design – _____________design
Stepwise refinement: __________ design
© Chinese University, CSE Dept. Software Engineering / 5 - 5
Top-down vs. Bottom-up Designs
Top-down design Bottom-up design
© Chinese University, CSE Dept. Software Engineering / 5 - 6
Software Design Activity
Software design– system decomposition into ____________
Description of a)functions of each module => ____-module
b)interfaces between modules => ____-module
the software structure (architecture)
© Chinese University, CSE Dept. Software Engineering / 5 - 7
Implement
Design for change – A design that is flexible enough to accommodate
changes easily
e.g. “___________” for constant names in C
Software Design Activity
M
M
M
M
Decompose
1
2
3
© Chinese University, CSE Dept. Software Engineering / 5 - 8
What Changes?
Change of algorithms– to improve efficiency, generality– e.g. sorting algorithm (____________ ____________)
Change of data structure (17% maintenance cost) – e.g. arrays vs linked lists– Data structures representing a Tree
Change of underlying abstract machine– Hides details of the underlying h/w– e.g. complier, database, OS
Change of peripheral devices– e.g. printers, terminals, Microsoft experience
Change of social environment– e.g. tax, social habits, currency changes
© Chinese University, CSE Dept. Software Engineering / 5 - 9
What Changes?
Change of algorithms– to improve efficiency, generality– e.g. sorting algorithm (____________ ____________)
Change of data structure (17% maintenance cost) – e.g. arrays vs linked lists– Data structures representing a Tree
Change of underlying abstract machine– Hides details of the underlying h/w– e.g. complier, database, OS
Change of peripheral devices– e.g. printers, terminals, Microsoft experience
Change of social environment– e.g. tax, social habits, currency changes
© Chinese University, CSE Dept. Software Engineering / 5 - 10
Data Structure Representing a Tree (1)
© Chinese University, CSE Dept. Software Engineering / 5 - 11
Data Structure Representing a Tree (2)
© Chinese University, CSE Dept. Software Engineering / 5 - 12
Product Families
A set of different versions of a program, due to– different h/w or s/w
– different environment with different requirements on performance or resource consumption
– system evolution due to new requirements or quality improvement
– Usually with the same basic set of functional requirements
© Chinese University, CSE Dept. Software Engineering / 5 - 13
Product Families
The solution:– minimize the cost of changing design decisions
Necessary tools to manage product families:
» ____________________________ tools
© Chinese University, CSE Dept. Software Engineering / 5 - 14
Sequential Completion:A Graphical View
Requirements
1
2
3
Version 1
Version 1
Version 2 5
Requirements
1
2
3
4 6
7 Version 3
4
Requirements
1
2
3
Version 2 5
Version 1
4
____________
__________
© Chinese University, CSE Dept. Software Engineering / 5 - 15
Modularization Techniques
Architecture design (overall structure) Detailed design (design of each module by
“information hiding”) WBS (Work Breakdown Structure):
– A description of the product hierarchy
© Chinese University, CSE Dept. Software Engineering / 5 - 16
Software Work Breakdown Structure: Product Hierarchy
Part of the final product
Describe how the work is to
be done
Software System (SS)
Subsystem (SS N)Subsystem (SS A) System (SS)activities
Subsystem(SS AA)
Subsystem(SS AM)
Subsystem (SS A)activities
Subsystem(SS AAA )
Subsystem(SS AAK )
Subsystem (SS AA)activities
SAAA SAAK SAA1-SAA6
SAA SAM SA1-SA6
SA SN S1-S6
S
© Chinese University, CSE Dept. Software Engineering / 5 - 17
(1) Overall Structure
Module - a well-defined component of a s/w system– self-contained
– with clear boundary
– collection of» Routines (.c)
» Data
» Type_definitions (.h)
© Chinese University, CSE Dept. Software Engineering / 5 - 18
(1) Overall Structure
Notationlet S be a s/w system, composed of modules M1, M2 ,…, Mn
i.e., S = { M1,M2,…,Mn }
A relation r on S is a subset of S×S
( cardinality = ______________ )
i.e., r: module × module Boolean
We say Mi is related to Mj (both in S) iff Mi r Mj is true.
( e.g. r: “call” )
© Chinese University, CSE Dept. Software Engineering / 5 - 19
(1) Overall Structure
The relations for modules i. ___________: ¬(Mi r Mi)
ii. contain _______________: Mi r+ Mj
iff Mi r Mj or Mk Mi r Mk and Mk r+ Mj
iii. ________(________):if Mi r+ Mj ¬(Mj r
+ Mi)
A hierarchy relation means there are no __________ in the graph of the relation
» directed acyclic graph (DAG) » vs. general graph (Figuer 5.1)
© Chinese University, CSE Dept. Software Engineering / 5 - 20
(1) Overall Structure
General Graph Directed Acyclic Graph (DAG)
Level___
___
___
___
___
M1
M2M3
M4
M1,1 M1,2 M1,3
M1,2,1 M1,2,2
M1,2,1,1
M
M M
M M
M
1
2 3
4 5
6
a) b)
Figure 5.1 Graph representation of a relation among modules, (a) General graph, (b) Direct acyclic graph (DAG)
© Chinese University, CSE Dept. Software Engineering / 5 - 21
Levels in a Hierarchy Relation
– LEVEL0: {Mi | Mj S Mi r Mj}
“_________________________________________________________________________________________
_________________________________________”
– LEVELk+1: {Mi | Mj LEVELk Mi r Mj and Mm
S, Mi r Mm Mm LEVELm, m [0,…,k] }
“_________________________________________________________________________________________
_____________________________”
– module Mi is “higher level” than module Mj iff Mi
LEVELi and Mj LEVELj and i > j
© Chinese University, CSE Dept. Software Engineering / 5 - 22
The USES Relation
–For Mi, Mj S, Mi USES Mj iff correct
execution of Mj is necessary for Mi to be
correct »Mi is a client of Mj
»An obvious USES example is when Mi and its
correctness depend on a call to a procedure in module Mj
–Q: Is _________________ equivalent to __________________ relation?
© Chinese University, CSE Dept. Software Engineering / 5 - 23
USES Is Not Equivalent to Calls
a) procedure calls are not the only way for modules to interact (Not all USES are calls)
−E.g.1 in assembly, a module may modify local data or instructions of another module
−E.g.2 _______________________
C: _________________
Fortran: _____________
General: __________Variables
write read
© Chinese University, CSE Dept. Software Engineering / 5 - 24
USES Is Not Equivalent to Calls
– E.g.3 exchange of control information (_____)
– E.g.4 in sequential environment, modules may communicate via parameter passing through subprograms
– E.g.5 in concurrent environment, modules may communicate via messages passing through tasks
b)Not all calls are USES» E.g. calling for reporting exceptions
© Chinese University, CSE Dept. Software Engineering / 5 - 25
USES Relation
USES relation is static, i.e., independent of the execution of the s/w:in Module M:
“if cond then proc1 else proc2”
in M1 in M2 “ “ USES
© Chinese University, CSE Dept. Software Engineering / 5 - 26
USES Relation
For a good design, USES relation should be hierarchy
i. easy to understand− “separation of concerns” => used components first, then their
clients
ii. easy to implement − “incrementality” otherwise, “we may end up with a system
in which nothing works until everything works.”
iii. easy to define structure− “levels of abstraction” high-level modules (clients) first,
then low-level (used) modules
(iii) _________________ design (i)(ii) ________________ implementation
© Chinese University, CSE Dept. Software Engineering / 5 - 27
How to Build Hierarchy Relations?
Key: decoupling modules to avoid recursion among modules.
1. recursion within a module does not affect the relation among modules
2. consider an alternative algorithm which does not require recursion
3. if two modules have to USES each other recursively, group them together into one large module
© Chinese University, CSE Dept. Software Engineering / 5 - 28
How to Build Hierarchy Relations?
4. consider splitting one module into two:
M M3M
M3
© Chinese University, CSE Dept. Software Engineering / 5 - 29
IS_COMPONENT_OF Relation
Let S be a set of modules M1,M2,…,Mn
for any Mi,Mj S, Mi IS_COMPONENT_OF Mj means
“Mj is realized by several modules, one of which being Mi”
Mj COMPRISES Mi
Let Ms,i be a subset of S where
Ms,i = { Mk | Mk S and Mk IS_COMPONENT_OF Mi}
then we say
Mi IS_COMPOSED_OF Ms,i or Ms,i IMPLEMENTS Mi
© Chinese University, CSE Dept. Software Engineering / 5 - 30
• M7 is component of _____
• _______ comprises M7
• _______________ implements M2
• M2 is composed of ___________________
M1
M M
M MM M M
2 4
5 67 8 9
M 3
M MM M M5 67 8 9
M2 M 3 M4
M1
(IS_COMPONENT_OF) (COMPRISES)
Figure 5.2 An example of the IS_COMPONENT_OF relation
© Chinese University, CSE Dept. Software Engineering / 5 - 31
Notations for a Design
In a design, once Mi is decomposed into the
set Ms,i, it is replaced by them in the
implementation; Mi becomes merely an
abstraction for that implementation Physically, the final implementation only
contains the “leaf” modules in a COMPRISES relation graph
© Chinese University, CSE Dept. Software Engineering / 5 - 32
Product Families Revisited
Is-composed-of
M3,3M3,2M3,1
M3
Uses
M7
M3
M5M6
M2 M4
M1
Uses
Version 1
Design Decision 1
Design Decision 2
Version 2Uses
M1
_______M4
_______ _______
M2
M1
_______M4
_______ _______
M2
© Chinese University, CSE Dept. Software Engineering / 5 - 33
Design Level Visibility
Data flow level
Structure chart level
“______________”
© Chinese University, CSE Dept. Software Engineering / 5 - 34
A Good Design Hierarchy
1. Partition the design: ________ vs ________
2. there is more than one level
3. there aren’t too many levels
4. for each Mi S, the set of modules
{Mj | Mi r Mj } to which it USES is small
“low ________” – number of outgoing edges of a module
( example )
© Chinese University, CSE Dept. Software Engineering / 5 - 35
A Good Design Hierarchy
5. for each Mi S, the set of modules (clients)
{Mj | Mj r Mi} which USES Mi is large
“high _______” – number of incoming edges of a module
6. ______ cohesion– internal connections in a module
______ coupling– external connections among modules
© Chinese University, CSE Dept. Software Engineering / 5 - 38
Interface, Implementation, and Information Hiding
In the USES relation, the set of services that each module provides to its clients is called ________________– The services are exported by the used modules and
imported by the clients
The interface is an _____________ of the module as viewed by its clients
A designer of module M only needs to know the interfaces of other modules used by M, and may ignore their ________________.
© Chinese University, CSE Dept. Software Engineering / 5 - 39
Interface, Implementation, and Information Hiding
Interface – _________________________– _________________________– _________________________
In summary, the clients of a module know about its services only through its interface, the implementation is hidden from them Information hiding, an important concept for software engineering principles
© Chinese University, CSE Dept. Software Engineering / 5 - 40
How to Design Interfaces Facilitating Information Hiding?
The interfaces should be as simple as possible, but not simpler
identify changeable design decisions and unchangeable module informationi. unchangeable information
________________ (stable)
ii. changeable design decisions “secret” (hidden in the ________________)
We say such information is ________________ within the module implementation.
© Chinese University, CSE Dept. Software Engineering / 5 - 41
Typical Secrets
1. The algorithm for carrying out some operation, e.g. algorithms for sorting routine
2. The representation of some data structure, e.g. abstract data type
– Clients know how to operate it, but do not know how it is implemented
3. The details of an interface to the abstract machine underlying the s/w
4. The policy for allocating some resource or ordering certain operations (e.g., PRINT a sorted list)
5. Abstract data type
© Chinese University, CSE Dept. Software Engineering / 5 - 42
Abstract Data Type
Abstract data type is an information hiding module where the representations of data structures are encapsulated.
– e.g. A “stack” could be defined in only four operations in its interface: _____,____,____, init
© Chinese University, CSE Dept. Software Engineering / 5 - 43
A Note About Architectural Design
A precise definition of the interface is necessary for module (re)usability– High _________; High __________
The interface must contain all the information that is needed to characterize the module’s behavior, and nothing more– Low __________
© Chinese University, CSE Dept. Software Engineering / 5 - 44
Procedures for Architectural Design
1. Apply USES and IS_COMPONENT_OF Notations
2. Build hierarchy relations (avoid recursion)3. Identify modules with high cohesion and low
coupling4. Design interface5. Hide information6. Reduce design complexity => exercise
© Chinese University, CSE Dept. Software Engineering / 5 - 45
(2) Detailed Design
s/w design notations– formal in syntax
– informal in semantics
1. TDN (Textual Design Notation)
2. GDN (Graphical Design Notation)
© Chinese University, CSE Dept. Software Engineering / 5 - 46
TDN: Textual Design Notation
key sections (_________)– module
– uses
– exports » var, type, procedure (________)
– implementation » list internal components
– end
____________
__________ relation
__________
__________ relation
Examples (Figure 5.3, Figure 5.4, Figure 5.5)
© Chinese University, CSE Dept. Software Engineering / 5 - 47
Figure 5.3 A Sample Module Description
module Xuses Y, Zexport
var A: integer;type B: array (1..10) of real;procedure C(D: in out B; E in integer; F: in real)
______________________________
Implementation
______________________________is composed of R, T
end X
© Chinese University, CSE Dept. Software Engineering / 5 - 48
module Xuses Y, Zexport
var A: integer;type B: array (1..10) of real;procedure C(D: in out B; E in integer; F: in real)
Here is an optional natural language description of what A, B and C actually are, along with possible constraints or properties that clients need to know; for example we might specify that objects of type B sent to procedure C should be initialized by the client, and should never contain all zeros.
Implementation If needed, here are general comments about the rationale of the
modularization, hints on the implementation, etc.
is composed of R, Tend X
Figure 5.3 A Sample Module Description
© Chinese University, CSE Dept. Software Engineering / 5 - 49
Figure 5.4 Sample Components of Module X
module Ruses Yexport
var K: record … end;type B: array (1..10) of real;procedure C(D: in out B; E in integer; F: in real)
Implementation
…
end R
module T
uses Y, Z, R
exports var A: integer;
implementation
…
end T
© Chinese University, CSE Dept. Software Engineering / 5 - 50
Figure 5.5 An Example of a Module with Selective Import
module Wuses X imports(B,C),
XXexports …implementation
.
.
.end W
X exports _____________;only ________ are imported here; referred as ___________.
XX resources ____________.
© Chinese University, CSE Dept. Software Engineering / 5 - 51
TDN
A TDN design takes some time to comprehend
TDN can provide various levels of details – e.g., ______________ in great detail
The designer needs to check for consistency and completeness
The tedious checking procedure should be automated if possible
A more complicated example follows.
© Chinese University, CSE Dept. Software Engineering / 5 - 52
Example: A “MINI” Compiler
module COMPILERexports procedure MINI ( PROG: in file of char;
CODE: out file of char);MINI is called to compile the program stored in PROG and produce the object code in file CODE
implementationA conventional compiler implementation ANALYZER performs both lexical and syntactic analysis and
produces an abstract tree as well as entries in the symbol table:CODE_GENERATOR generates code starting from the abstract
tree and information stored in the symbol table. Module MAIN acts as a job coordinator.is composed of ANALYZER, SYMBOL_TABLE,
ABSTRACT_TREE_HANDLER,CODE_GENERATOR, MAIN
end COMPILER
© Chinese University, CSE Dept. Software Engineering / 5 - 53
Example: A “MINI” Compiler
module MAINuses ANALYZER, CODE_GENERATORexports procedure MINI(PROG: in file of char;
CODE: out file of char); .
.end MAIN
module ANALYZERuses SYMBOL_TABLE, ABSTRACT_TREE_HANDLERexports procedure ANALYZE(source :in file of char);
SOURCE is analyzed; an abstract tree is produced by using the services provided by the handler, and recognizedentities, with their attributes are stored in the symbol table.
end ANALYZER
© Chinese University, CSE Dept. Software Engineering / 5 - 54
Example: A “MINI” Compiler
module CODE_GENERATOR
uses SYMBOL_TABLE, ABSTRACT_TREE_HANDLER
exports procedure CODE(OBJECT: out file of char);
The abstract tree is traversed using the operations exported by the ABSTRACT_TREE_HANDLER and accessing the information stored in the symbol table in order to generate code in the output file.
.
.
.
end CODE_GENERATOR
© Chinese University, CSE Dept. Software Engineering / 5 - 55
GDN (Graphical Design Notation)
a box represents a ___________ incoming arrows represent a module’s
__________, i.e., exported ___________ not to be confused with DFD can also show “IS_COMPOSED_OF” and
“IS_COMPONENT_OF” relations
© Chinese University, CSE Dept. Software Engineering / 5 - 56
Figure 5.6 Graphical description of module X of Figure 5.3
Figure 5.6 GDN of Module X
X
Y
Z A B
R T Module Module
Module
Module
Module
C
© Chinese University, CSE Dept. Software Engineering / 5 - 57
Figure 5.7 X’s Decomposition
Figure 5.7 Module X is composed of modules R and T
X
Y
Z B C
R T Module Module
Module
Module
Module
A
K
© Chinese University, CSE Dept. Software Engineering / 5 - 58
Module M
Module L Module N
Module M
Module R
Module M
Module S
Module H Module G
Figure 5.8 Module M is a member of both L and N
A
B
K
Module M
IS_COMPONENT_OF what?
_________________
IS_COMPOSED_OF what?
_________________
Exports what?
_________________
USES what?
_______ (which exports _______ )Module F
© Chinese University, CSE Dept. Software Engineering / 5 - 59
GDN (Graphical Design Notation)
It provides “s/w design visualization” It is useful in comprehending complicated s/w
(e.g., concurrent s/w) It may take too much space It is useful for high-level view of the s/w design Detailed levels need to be automated (CASE)
– E.g. Statemate, Teamwork, Rational, UML
© Chinese University, CSE Dept. Software Engineering / 5 - 60
Categories of Modules
1) Procedural abstractions– purpose: to encapsulate an ____________
– e.g.: sorting module, fast Fourier transform module
2) Libraries– purpose: to group together related
___________________________
– e.g.: » mathematical routines
» graphical routines
© Chinese University, CSE Dept. Software Engineering / 5 - 61
Categories of Modules
3) Common pool of data– purpose: to share _______ among several
modules
– e.g.: system configuration constants group
– note:
© Chinese University, CSE Dept. Software Engineering / 5 - 62
Categories of Modules
4) Abstract objects– purpose: to hide the details of data __________
(change of data structures is _________ of cost for total software maintenance)
– e.g.: a symbol-table
– note: exhibit a state
© Chinese University, CSE Dept. Software Engineering / 5 - 63
Categories of Modules
5) Abstract data type– purpose: to hide the ___________ of a data type
and the algorithms used in ________ the objects of that type
– e.g.:» Stack» Ada - private type» Module-2 - opaque type
– note:i) no stateii) can generate many instances (___________)
© Chinese University, CSE Dept. Software Engineering / 5 - 64
Stack as an Abstract Data Type
Module STACK_HANDLERexports
type STACK : ?; This is an abstract data type module; the data structureIs a secret hidden in the implementation part.procedure PUSH(S: in out STACK; VAL: in element);procedure POP(S: in out STACK; VAL: out element);function EMPTY(S: in STACK): BOOLEAN;function TOP(S: in STACK): element;procedure INIT(S: out STACK);
end STACK_HANDLER
A new symbol to export a _______________, leaving implementation detail hidden.
© Chinese University, CSE Dept. Software Engineering / 5 - 65
Stack Implemented as an Array
stack: record sp: integer; arr: array[1..Max] of element end; procedure Push(e:element) is begin stack.sp := stack.sp + 1; stack.arr[stack.sp] := e; end; procedure Pop is begin
stack.sp := stack.sp -1; end; function Top return element is begin return stack.arr[stack.sp]; end; procedure Init is begin stack.sp := 0; end;
© Chinese University, CSE Dept. Software Engineering / 5 - 66
Stack Implemented as a List type stackptr;type stackelement is record elem: element; next: stackptr; end record;type stackptr is access stackelement;stack: stackptr;procedure Push(e:element) is item: stackptr; begin item := new stackelement;item := e;item.next := stack;stack := item; end;procedure Pop is begin
stack := stack.next; end;function Top return element is begin return stack.item; end;procedure Init is begin stack := null; end;
© Chinese University, CSE Dept. Software Engineering / 5 - 67
Yet Another Example of Abstract Data Type
module FIFO_CARSuses CARSexports
type QUEUE :?;procedure ENQUEUE(Q: in out QUEUE; C: in CARS);procedure DEQUEUE(Q: in out QUEUE; C: out CARS);function IS_EMPTY(Q: in QUEUE) :BOOLEAN;function LENGTH(Q: in QUEUE): NATURAL;procedure MERGE(Q1, Q2: in QUEUE; Q: out QUEUE);
end FIFO_CARS Declaration of the type QUEUE: gasoline_1, gasoline_2, gasoline_3 : QUEUE;car_wash :QUEUE; Operations on the type QUEUE: ENQUEUE(car_wash, that_car);MERGE(gasoline_1, gasoline_2, gasoline_3);
© Chinese University, CSE Dept. Software Engineering / 5 - 68
More Techniques for Design for Change
Configuration constants – Constants which might be changed due to
different configuration
– E.g., a :array(1..N) of integer;
N = 100;
– these configuration constants could be grouped into a module that provides
____________________
© Chinese University, CSE Dept. Software Engineering / 5 - 69
More Techniques for Design for Change Conditional compilation
– All versions of a program family are represented by one single source copy, and the differences between various versions are determined by condition compilation
– … {portion common to all versions}#ifdef h/w-1 {compiled portion if h/w–1 is true}#endif#ifdef h/w-2 {compiled portion if h/w–2 is true}#endif
Software Generator– yacc - compiler generator– user interface generator– executable specification language
© Chinese University, CSE Dept. Software Engineering / 5 - 70
Stepwise Refinement
a popular method for describing the logical structure of a given algorithm, implemented by a single module
stepwise refinement is an effective way of describing a solution after it has been partially obtained (software engineering principle: _______________)
© Chinese University, CSE Dept. Software Engineering / 5 - 71
Example: Derivation of Selection Sort
Step 1let n be the length of the array a to be sorted;i := 1 ;while i < n loop
find the smallest of ai .. .an, and exchange it with the element at position i;i := i + 1;
end loop;
© Chinese University, CSE Dept. Software Engineering / 5 - 72
Step 2let n be the length of the array a to be sorted;i := 1 ;while i < n loop
j := n;while j > i loop
if a(i) > a(j) theninterchange the elements at
positions j and i ;end if;j := j - 1;
end loop;i := i + 1;
end loop;
© Chinese University, CSE Dept. Software Engineering / 5 - 73
Step 3let n be the length of the array a to be
sorted;i := 1 ;while i < n loop
j := n;while j > i loop
if a(i) > a(j) thenx := a(i); a(i) := a(j); a(j) := x;
end if;j := j - 1;
end loop;i := i + 1;
end loop;
© Chinese University, CSE Dept. Software Engineering / 5 - 74
Stepwise Refinement
P 2
P
P 1 P 3
P 2,1
P 2,1,1 P 2,1,2
C
C 1 not C 1
Figure 4.13 Graphical representation of stepwise refinement.
[While C loop P2,1]]
[ If C1 then P2,1,1
Else P2,1,2]
Step 0
Step 1
Step 2
Step N
© Chinese University, CSE Dept. Software Engineering / 5 - 75
Stepwise Refinement
It is a method that works in the small, but fails in the large. Here are the reasons:
1. sub-problems tend to be analyzed in isolation, reducing ______________
2. No attention is paid to _______________
3. No attention is paid to _______________
4. The ________ function may not exist
5. A premature commitment to the ____________ structures
© Chinese University, CSE Dept. Software Engineering / 5 - 76
A Short Summary About Design
stepwise refinement– top-down design
information hiding– bottom-up design
combining both– yo-yo design
1. design should use both schemes
2. description of the resulting design should be ______________
© Chinese University, CSE Dept. Software Engineering / 5 - 77
Handling Anomalies
To achieve reliable solutions– Systematic design approach
– Rigorous and disciplined implementation
One solution: employ __________ design – Purpose: build robust systems
– The program should continue to behave reasonably even in the case of unexpected and unforeseen circumstances
© Chinese University, CSE Dept. Software Engineering / 5 - 78
Handling Anomalies
Anomalous – if a module fails to provide a service as expected and specified in its interface– _________ – to be associated with a service,
denoting the anomalies that may occur while that service is being performed
© Chinese University, CSE Dept. Software Engineering / 5 - 79
Handling Anomalies
Defensive design requires that upon entering an anomalous state, the module should signal the anomaly by raising an exception to the client
The server module terminates the service after _________ an exception; the client responds by suitably ________ the exception
© Chinese University, CSE Dept. Software Engineering / 5 - 80
Why an Exception is Raised by a Module, say M?
1. M’s client does not satisfy the required protocol for invoking one of M’s service
2. When providing a service to its client, M does not invoke a service exported by another module properly
3. M fails to provide its service because of an unforeseen condition
© Chinese University, CSE Dept. Software Engineering / 5 - 81
M’s server
M
M’s client
__
__
__
M’s server
C;
M’s clientA
B
C
B;
______ an exception
______ an exception
_____ the exception
M:
© Chinese University, CSE Dept. Software Engineering / 5 - 82
Module M
module Mexports . . .
procedure P (X: INTEGER; . . .) raises X_NON_NEGATIVE_EXPECTED,
INTEGER_OVERFLOW;X is to be positive; if not, exceptionX_NON_NEGATIVE_EXPECTED is raised;INTEGER_OVERFLOW is raised if internalcomputation of P generates an overflow
.
.
.
end M
© Chinese University, CSE Dept. Software Engineering / 5 - 83
Module L
module L
uses M imports P (X: INTEGER; . .) .) exports . . .;
procedure R ( . . .) raises INTEGER_OVERFLOW;
.
.
. implementation
If INTEGER_OVERFLOW is raised when P is invoked, the
exception is propagated . . .
end L
© Chinese University, CSE Dept. Software Engineering / 5 - 84
Concurrent Software
The case of a module defining shared data E.g., abstract object BUFFER
– module QUEUE_OF_CHAR is GENERIC_FIFO_QUEUE (CHAR)
– BUFFER : QUEUE_OF_CHAR.QUEUE
with operations– PUT: inserts a character in BUFFER– GET: extracts a character from BUFFER – NOT_FULL: returns true if BUFFER not full – NOT_EMPTY: returns true if BUFFER not empty
© Chinese University, CSE Dept. Software Engineering / 5 - 85
How to Control Correct Access to Shared Data?
Not sufficient that clients check operation invocations, such as
if QUEUE_OF_CHAR.NOT_FULL (BUFFER) then QUEUE_OF_CHAR.PUT (X, BUFFER);
end if;
Consumer_1 and Consumer_2 might do this concurrently
» if only one slot is left, both may find the buffer not full, the first who writes fills it, and the other writes in a full buffer
© Chinese University, CSE Dept. Software Engineering / 5 - 86
Enforcing Synchronization
Ensure that operations on buffer are executed in mutual exclusion
Ensure that operations such asif QUEUE_OF_CHAR.NOT_FULL (BUFFER) then
QUEUE_OF_CHAR.PUT (X, BUFFER);
end if;
are executed as logically non-interruptible units
© Chinese University, CSE Dept. Software Engineering / 5 - 87
Monitors
Abstract objects used in a ____________ environment
Available in the Java programming language
© Chinese University, CSE Dept. Software Engineering / 5 - 88
Monitors: an Example
concurrent module CHAR_BUFFER This is a monitor, i.e., an abstract object module in a concurrent environment
uses . . . exports
procedure PUT (C : in CHAR) requires NOT_FULL; procedure GET (C: out CHAR) requires NOT_EMPTY; NOT_EMPTY and NOT_FULL are hidden Boolean functions yielding TRUE if the buffer is not empty and not full, respectively. They are not exported as operations, because their purpose is only to delay the calls to PUT and GET if they are issued when the buffer is in a state where it cannot accept them . . .
end CHAR_BUFFER
© Chinese University, CSE Dept. Software Engineering / 5 - 89
Comments on Monitors
Monitor operations are assumed to be executed in _________________
A requires clause may be associated with an operation– it is automatically checked when operation is
called– if the result is false, the current process is
suspended until it becomes true (at that stage it becomes eligible for resumption)
© Chinese University, CSE Dept. Software Engineering / 5 - 90
Monitor Types: an Example
generic concurrent module GENERIC_FIFO_QUEUE (EL) This is a generic monitor type, i.e., an abstract data type accessed in a concurrent environment
uses . . . exports
type QUEUE: ?; procedure PUT (Q1: in out QUEUE; E1: in EL)
requires NOT_FULL (Q1: QUEUE); procedure GET (Q2: in out QUEUE; E2: out EL)
requires NOT_EMPTY(Q2: QUEUE); . . .
end GENERIC_FIFO_QUEUE (EL)
© Chinese University, CSE Dept. Software Engineering / 5 - 91
Guardians and Rendez-vous
The Ada style of designing concurrent systems
In Ada a shared object is ___________ (whereas a monitor is ____________)– it is managed by a guardian process which can
accept rendez-vous requests from tasks willing to access the object
© Chinese University, CSE Dept. Software Engineering / 5 - 92
A Guardian Task
loopselect
when NOT_FULL accept PUT (C: in CHAR);This is the body of PUT; the client calls it as if itwere a normal procedureend ;
orwhen NOT_EMPTY
accept GET (C: out CHAR);This is the body of GET; the client calls it as if itwere a normal procedure
end ;end select ;
end loop ;
note _______________ acceptance ofrendez-vous requests
© Chinese University, CSE Dept. Software Engineering / 5 - 93
Real-Time Software
A case where processes interact with the _______________
E.g., a put operation on a shared buffer is invoked by a plant sensor sending data to a controller– plant cannot be suspended if buffer full!
» design must ensure that producer never finds the buffer full
» this constrains the speed of the consumer process in the controller
© Chinese University, CSE Dept. Software Engineering / 5 - 94
TDN Description
concurrent module REACTIVE_CHAR_BUFFER This is a monitorlike object working in a real-time environment. uses . . . exports
reactive procedure PUT (C: in CHAR); PUT is used by external processes, and two consecutive PUT requests must arrive more than 5 msec apart; otherwise, some characters may be lost procedure GET (C: out CHAR); . . .
end REACTIVE_CHAR_BUFFER
© Chinese University, CSE Dept. Software Engineering / 5 - 95
GDN Description
Module
REACTIVE_CHAR_BUFFER
PUT GET
zig-zag arrow indicates ______________ invocation
© Chinese University, CSE Dept. Software Engineering / 5 - 96
Distributed Software
Issues to consider– module-machine binding
– intermodule communication» e.g., remote procedure call or message passing
– access to shared objects» may require replication for efficiency reasons
© Chinese University, CSE Dept. Software Engineering / 5 - 97
Client-Server Architecture
The most popular distributed architecture Server modules provide services to client
modules Clients and servers may reside on different
machines
© Chinese University, CSE Dept. Software Engineering / 5 - 98
Issues
Binding modules to machines– static vs. dynamic (migration)
Inter-module communication– e.g., Remote Procedure Call (RPC)
– To define interface of remote procedures
Replication and distribution
© Chinese University, CSE Dept. Software Engineering / 5 - 99
Middleware
Layer residing between the network operating system and the application
Helps building network applications Provides useful services
– _____________ services, to find processes or resources on the network
– ______________ services, such as message passing or RPC (or RMI)
© Chinese University, CSE Dept. Software Engineering / 5 - 100
A Further Relation: Inheritance (Generalization – Specialization)
The keyword in (pure) object-oriented design: “inheritance” - to form a class of objects. – e.g., “class” in C++ (public, private, protected)
– e.g., “extends” in Java creates “subclasses”
ADTs may be organized in a hierarchy Class B may specialize class A
– B inherits from A
conversely, A generalizes B A is a superclass of B B is a subclass of A
© Chinese University, CSE Dept. Software Engineering / 5 - 101
Key Concept in O-O Design: Inheritance A new relation: INHERITS_FROM If M1 INHERITS_FROM M2, then M1 has visibility into
the internal structure (the _______) of M2 a hierarchy relation
1. _____________________
2. _____________________
3. _____________________
M1 INHERITS_FROM M2
– M2: parent module
– M1: heir module
© Chinese University, CSE Dept. Software Engineering / 5 - 102
An Inheritance Example in TDN
class EMPLOYEE exports
function FIRST_NAME(): string_of_char; function LAST_NAME(): string_of_char; function AGE(): natural; function WHERE(): SITE; function SALARY: MONEY; procedure HIRE (FIRST_N: string_of_char;
LAST_N: string_of_char; INIT_SALARY: MONEY);
Initializes a new EMPLOYEE, assigning a new identifier. procedure FIRE(); procedure ASSIGN (S: SITE); An employee cannot be assigned to a SITE if already assigned to it (i.e., WHERE must be different from S). It is the client’s responsibility to ensure this. The effect is to delete the employee from those in WHERE, add the employee to those in S, generate a new id card with security code to access the site overnight, and update WHERE.
end EMPLOYEE
Figure 5.9 An Example of Inheritance: TDN
© Chinese University, CSE Dept. Software Engineering / 5 - 103
class ADMINISTRATIVE_STAFF inherits EMPLOYEE exports
procedure DO_THIS (F: FOLDER); This is an additional operation that is specific to administrators; other operations may also be added.
end ADMINISTRATIVE_STAFF class TECHNICAL_STAFF inherits EMPLOYEE exports
function GET_SKILL(): SKILL; procedure DEF_SKILL (SK: SKILL); These are additional operations that are specific to technicians; other operations may also be added.
end TECHNICAL_STAFF
Figure 5.9 An Example of Inheritance: TDN
© Chinese University, CSE Dept. Software Engineering / 5 - 104
An Example of Inheritance: GDN
Module EMPLOYEE
Module
ADMINSTRATIVE
STAFF
Module
TECHNICAL
STAFF
GET_SKILLDEF_SKILL
DO_THIS
NAMEAGE
WHEREModule SITE
... to modules MONEY
exportsuses
_________
________
_____________________
Module
FOLDER
Module
SKILL
________
© Chinese University, CSE Dept. Software Engineering / 5 - 105
Properties of Inheritance
Inheritance allows a hierarchical classification scheme for abstract data types through generalization-specialization relations– distinguish commonalities and differences among
a (potential) set of abstract objects
– commonalities are factored out in a ________ class
– variations are singled out in the ___________ classes
© Chinese University, CSE Dept. Software Engineering / 5 - 106
Properties of Inheritance
Inheritance improves reusability - ________ class
Inheritance is a way of building software ______________
Disadvantage of inheritance
© Chinese University, CSE Dept. Software Engineering / 5 - 107
Properties of Inheritance
A subclass defines a subtype– subtype is substitutable for parent type
Polymorphism– a variable referring to type A can refer to an object
of type B if B is a subclass of A
Dynamic binding – the method invoked through a reference depends on
the type of the object associated with the reference at runtime
© Chinese University, CSE Dept. Software Engineering / 5 - 108
Several (Controversial) Extensions
Redefinition: an heir product provides a different implementation for one of its parent’s services – e.g., change of the sorting algorithm regarding its implementation
(no change to interface) – _______ and _________ allow it; ________ does not
Redefinition applied to the data structure hidden in the parent module
Multiple inheritance: define a new module by combining the feature provided by two or more parent modules – e.g., FIFO_QUEUE, EMPLOYEE– ________ allows it, but _________ and _________ do not
© Chinese University, CSE Dept. Software Engineering / 5 - 109
Using UML in Software Design
UML (Unified Modeling Language) notation is popular in software engineering.
UML is a collection of languages that provide specific notations to specify, analyze, visualize, construct, and ________ software.
UML is a widely adopted standard notation for representing OO designs
We introduce the UML class diagram– classes are described by boxes
© Chinese University, CSE Dept. Software Engineering / 5 - 110
UML Representation of Inheritance
EMPLOYEE
TECHNICAL_STAFF ADMINISTRATIVE_STAFF
Note: similar to __________
© Chinese University, CSE Dept. Software Engineering / 5 - 111
UML Associations Associations are relations that the
implementation is required to support Can have multiplicity constraints
TECHNICAL _STAFF
MANAGER
PROJECT * 1 project_member
1
1..* manages
© Chinese University, CSE Dept. Software Engineering / 5 - 112
UML Aggregation
Defines a PART_OF relationDiffers from IS_COMPOSED_OF
Here TRANGLE has its own methods
It implicitly uses POINT to define
its data attributes
TRIANGLE
POINT
1
3
© Chinese University, CSE Dept. Software Engineering / 5 - 113
More on UML
UML evolves from TDN/GDN USES relationship is refined with semantically richer relationship: inheritance,
association, and aggregation– If class B inherits from class A, B USES A => ___________ USES _________– Association between classes implicitly define USES relation => ___________ USES
____________– If B aggregates A, B USES A => __________ USES __________
Representation of IS_COMPONENT_OF via the package notation
package_name
Class 1
Class 2
Class 3
© Chinese University, CSE Dept. Software Engineering / 5 - 114
Software Architecture
Describes overall system organization and structure in terms of its major constituents and their interactions
Standard architectures can be identified– pipeline
– blackboard
– event based (publish-subscribe)
© Chinese University, CSE Dept. Software Engineering / 5 - 115
Standard Architectures
PipelineExample: _________________
event basedExample: _______________
_______________________
BlackboardExample: ____________ ,
_________________
© Chinese University, CSE Dept. Software Engineering / 5 - 116
Domain Specific Architectures
"model–view–controller" architecture for software that has a significant amount of user interaction
Model (store data e.g. text)
Controller (interact with user; perform commands)
View (display model for user)
© Chinese University, CSE Dept. Software Engineering / 5 - 117
Software Components
Goal– build systems out of pre-existing libraries of
components
– as most mature engineering areas do
Examples– STL (Standard Template Library) for C++
– JavaBeans and Swing for Java
– .NET (including COM objects and DLL libraries)
© Chinese University, CSE Dept. Software Engineering / 5 - 118
Component Integration
The CORBA (Common Object Request Broker Architecture) Middleware
Clients and servers connected via an Object Request Broker (ORB)
Interfaces provided by servers defined by an Interface Definition Language (IDL)
Existing paradigm: Web Services Current trend: Cloud Computing + Mobile Apps
© Chinese University, CSE Dept. Software Engineering / 5 - 119
Architectures for Distributed Systems
From two tiered– Client-server
to three tiered
Requests for service (database)
Web browser
(client)
Web server (server) Requests
for service (pages)
User interface
(client)
Decode
service
request (2nd tier)
Application
server (databse)
© Chinese University, CSE Dept. Software Engineering / 5 - 120
Program Implementation Techniques
1. Programming Standards and Procedures Need for standards and procedures
A uniformed good programming style helps understandability, consistency, correctness of a team project.
a) Standards for You Standards and procedures help you ____________ your thoughts and avoid mistakes; they also help in ____________ design to code
b) Standards for OthersIt is essential you organize, format, and document your code to make it easy for others to understand what it does and how it works.
© Chinese University, CSE Dept. Software Engineering / 5 - 121
Program Implementation Techniques
Matching _______ with _______________– Direct correspondence
between the program design modules and the program code modules
– Keep up with the good design for traceability and maintainability
– Use header file to trace module design
/***************************************** MODULE TO FIND INTERSECTION OF TWO LINES * MODULE NAME: FINDPT* PROGRAMMER: HU-AIM EYE* VERSION: 1.0 (3 MARCH 15)** PROCEDURE INVOCATION:* CALL FINDPT (A1, B1, C1, A2, B2, C2, XS, YS, FLAG)** INPUT PARAMETERS:* INPUT LINES ARE OF THE FORM* A1*X + B1*Y + C1 = 0 AND* A2*X + B2*Y + C2 = 0* SO INPUT IS COEFFICIENTS A1, B1, C1 AND* A2, B2, C2* * OUTPUT PARAMETERS:* IF LINES ARE PARALLEL, FLAG SET TO 1.* ELSE FLAG = 0 AND POINT OF INTERACTION* IS (XS, YS).*****************************************/
© Chinese University, CSE Dept. Software Engineering / 5 - 122
Program Implementation Techniques
2. Programming Guidelines– Control structures
– Algorithms
– Data structures
– General guidelines
© Chinese University, CSE Dept. Software Engineering / 5 - 123
Control Structures
Using Fundamental Constructs
Main ingredients for structured programs:
1. ______________e.g.,___________________
2. ______________e.g.,___________________
3. ______________e.g.,___________________
© Chinese University, CSE Dept. Software Engineering / 5 - 125
__________
OR
Control Structures
____________ ____________
© Chinese University, CSE Dept. Software Engineering / 5 - 126
Control Structures
Top-down FlowPrograms should be readable from the top down
Use of Submodules– Follow the design to build submodules (functions,
procedures, macros) for elemental functions
– Build general submodules for ____________
– Explain _________ of submodules, but not their details
© Chinese University, CSE Dept. Software Engineering / 5 - 127
Program Without Top-Down Flow
BENEFIT = MINIMUMIF (AGE < 75) GO TO A;BENEFIT = MAXIMUM;GO TO C;IF (AGE < 65) GO TO B;IF (AGE < 55) GO TO C;A:IF (AGE < 65) GO TO B;
BENEFIT = BENEFIT * 1.5 + BONUS;GO TO C;
B:IF (AGE < 55) GO TO C;BENEFIT = BENEFIT * 1.5;
C:Next statement
© Chinese University, CSE Dept. Software Engineering / 5 - 128
Program With Top-Down Flow
IF (AGE < 55) THEN BENEFIT = MINIMUM;ELSE IF (AGE < 65) THEN BENEFIT = MINIMUM + BONUS;ELSE IF (AGE < 75) THEN BENEFIT = MINIMUM * 1.5 +
BONUS;ELSE BENEFIT = MAXIMUM;
© Chinese University, CSE Dept. Software Engineering / 5 - 129
Algorithms
Program design often specifies a class of algorithms to be used
Efficiency of code should consider 1. ____________ of code
2. time to ______ the code
3. time to ______ the code
4. time for users to ___________ the code
5. time to ______ the code, if necessary
© Chinese University, CSE Dept. Software Engineering / 5 - 130
Data Structures
Keeping the Program Simple. Restructuring data can simplify a program.– Keep It Simple and Straightforward: _________
Using the Structure of Data to Determine the Structure of the Program
Localizing Input and Output in Separate Modules
© Chinese University, CSE Dept. Software Engineering / 5 - 131
General Guidelines
Using Pseudocode Revising and Rewriting Instead of Patching
© Chinese University, CSE Dept. Software Engineering / 5 - 132
Example of Program Simplification Using Data
Computing a tax due by: 1. For the first $10,000 of income, the tax is 10%. 2. For the next $10,000 above $10,000, the tax is
12%. 3. For the next $10,000 above $20,000, the tax is
15%. 4. For the next $10,000 above $30,000, the tax is
18%. 5. For any income above $40,000, the tax is 20%.
© Chinese University, CSE Dept. Software Engineering / 5 - 133
Program 1
TAX = 0.IF TAXABLE_INCOME = 0, GO TO EXITIF TAXABLE_INCOME > 10,000, TAX = TAX + 1000.
ELSE TAX = TAX + 0.10 * TAXABLE_INCOMEGO TO EXIT
IF TAXABLE_INCOME > 20,000, TAX = TAX + 1200.ELSE TAX = TAX + 0.12*(TAXABLE_INCOME – 10000.)GO TO EXIT
IF TAXABLE_INCOME > 30,000, TAX = TAX + 1500.ELSE TAX = TAX + 0.15*(TAXABLE_INCOME – 20000.)GO TO EXIT
IF TAXABLE_INCOME < 40,000, TAX = TAX + 0.18*(TAXABLE_INCOME – 30000.)GO TO EXIT
ELSE TAX=TAX+1800.+0.20*(TAXABLE_INCOME–40000.)EXIT: END
© Chinese University, CSE Dept. Software Engineering / 5 - 134
Program 2
Build a Sample Tax Table First:Bracket Base Percent0 0 1010,000 1000 1220,000 2200 1530,000 3700 1840,000 5500 20 The Program:LEVEL = 1
FOR I = 1 TO 4 DOIF ______________________________
THEN ___________________________
TAX = ______________________________________________________
© Chinese University, CSE Dept. Software Engineering / 5 - 135
Program Implementation Techniques
3. Documentation Internal Documentation (documentation
associated with the program code)– Header Comment Block– Other Program Comments
– Meaningful Variable Names and Statement Labels
© Chinese University, CSE Dept. Software Engineering / 5 - 136
Header Comment Block
Specify 6 W’s 1. ______ your program is
2. ______ wrote the program
3. ______ the program fits in the general system design
4. ______ the program was written and revised
5. ______ the program exists
6. ______ your program uses its data structures, algorithms, and control
© Chinese University, CSE Dept. Software Engineering / 5 - 137
Sample
PROGRAM SCAN – Program to scan a line of text for a given character
PROGRAMMER: Wan, Lai-man 2609 8481CALLING SEQUENCE: CALL SCAN(LENGTH,CHAR)Where ‘LENGTH’ is the length of the line to be scanned, ‘CHAR’ is
the character to be sought, line of text passed as array ‘NTEXT’VERSION 1: written 2-2-2010REVISION 1.1: 3-3-2010 to improve searching algorithm.PURPOSE: General-purpose scanning module to be used for each
new line of text, no matter the length. DATA STRUCTURES: Variable LENGTH – INTEGER
Variable CHAR – CHARACTERARRAY NTEXT – CHARACTER array of length ‘LENGTH’
ALGORITHM: Reads array NTEXT one character at a time; if CHAR is found, position in NTEXT returned in variable ‘LENGTH’;
else variable ‘LENGTH’ set to 0
__________________
______
______
______
© Chinese University, CSE Dept. Software Engineering / 5 - 138
Program Implementation Techniques
– Formatting to Enhance Understanding » _________________________________
» _________________________________
– Documenting Data
» _________________________________
© Chinese University, CSE Dept. Software Engineering / 5 - 139
Program Implementation Techniques
External Documentation – Describing _________________ (what)
– Describing _________________ (how)
– Describing _________________ (how)
© Chinese University, CSE Dept. Software Engineering / 5 - 140
Topic 5 Conclusion
TDN, GDN Modularization techniques Information hiding Hierarchical relations Abstract objects and abstract data types Handling Anomalies Concurrent, real-time, distributed systems Architecture and components Program Implementation Techniques