Software Design
Software Design
Software Design
Design Patterns
• A design pattern
• Is a common solution to a recurring problem in design
• Abstracts a recurring design structure
• Comprises class and/or object
• Dependencies
• Structures
• Interactions
• Conventions
• Names & specifies the design structure explicitly
• Distils design experience
Re-use Code VS. Design
• Code re-use
• Don’t reinvent the wheel
• Requires clean, elegant, understandable, general, stable code
• leverage previous work
• Design re-use
• Don’t reinvent the wheel
• Requires a precise understanding of common, recurring designs
• leverage previous work
Patterns vs “Design”
• A Pattern must:
• Solve a problem and be useful
• Have a context and can describe where the solution can
be used
• Recur in relevant situations
• Provide sufficient understanding to tailor the solution
• Have a name and be referenced consistently
Description of a Design Pattern
• They are:
• “Descriptions of communicating objects and classes that
are customized to solve a general design problem in a
particular context.”
Characteristics of Good Design 28
• Component independence
• High cohesion
• Low coupling
• Exception identification and handling
• Fault prevention and fault tolerance
• Design for change
M1 M2
M3
Design Principles
• Abstraction
• Modularity, coupling and cohesion
• Information hiding
• Limit complexity
• Hierarchical structure
Abstraction 30
abstraction subproblems
time
SE, Design, Hans van Vliet, ©2008
Abstraction 31
simpler data
general structure
data structures
• Definition
• The degree to which all elements of a component are directed
towards a single task.
• The degree to which all elements directed towards a task are
contained in a single component.
• The degree to which all responsibilities of a single class are related.
• Internal glue with which component is constructed
• All elements of component are directed toward and
essential for performing the same task.
33
Type of Cohesion 34
High Cohesion
Functional
Sequential
Communicational
Procedural
Temporal
Logical
Coincidental Low
34
Functional
Sequential
Communicational
Coincidental Cohesion 35
Procedural
Temporal
Logical
Coincidental
35
Functional
Sequential
Communicational
Logical Cohesion 36
Procedural
Temporal
Logical
Coincidental
36
Example 37
• A component reads inputs from tape, disk, and
network.
• All the code for these functions are in the same
component.
• Operations are related, but the functions are
significantly different.
Improvement?
37
Functional
Sequential
Communicational
Temporal Cohesion 38
Procedural
Temporal
Logical
Coincidental
38
Functional
Sequential
Communicational
Procedural Cohesion 39
Procedural
Temporal
Logical
Coincidental
39
Functional
Sequential
Communicational
Communicational Cohesion 40
Procedural
Temporal
Logical
Coincidental
40
Functional
Sequential
Communicational
Sequential Cohesion 41
Procedural
Temporal
Logical
Coincidental
41
Functional
Sequential
Communicational
Functional Cohesion 42
Procedural
Temporal
Logical
Coincidental
42
Examples of Cohesion 43
Function A Function A Time t0
Function Function
B C logic Function A’ Time t0 + X
Function Function
D E Function A’’
Time t0 + 2X
Coincidental Logical Temporal
Parts unrelated Similar functions Related by time
Function A
Function B
Function C
Procedural
Related by order of functions
43
Examples of Cohesion (Cont.)
44
Function A Function A
Function B Function B
Function C Function C
Communicational Sequential
Access same data Output of one is input to another
Function A part 1
Function A part 2
Function A part 3
Functional
Sequential with complete, related functions
44
How to determine the cohesion type? 45
High Coupling
Content Avoid
Common
External
Control
Loose
Stamp
48
Content
Common
External
Control
49
Example 50
Part of a program handles lookup for customer.
When customer not found, component adds
customer by directly modifying the contents of
the data structure containing customer data.
Improvement?
50
Content
Common
Common Coupling External
Control
Stamp
Data
Uncoupled
51
• Definition: More than one component share
data such as global data structures
• Usually a poor design choice because
• Lack of clear responsibility for the data
• Reduces readability
• Difficult to determine all the components that affect a
data element (reduces maintainability)
• Difficult to reuse components
• Reduces ability to control data accesses
51
Example
52
Process control component maintains current data
about state of operation. Gets data from multiple
sources. Supplies data to multiple sinks. Each source
process writes directly to global data store. Each sink
process reads directly from global data store.
Improvement?
52
Content
Common
External Coupling
External
Control
Stamp
Data
Uncoupled
53
• Definition: Two components share something
externally imposed, e.g.,
• External file
• Device interface
• Protocol
• Data format
• Improvement?
Content
Common
External
Control
54
Content
Common
External
Control
55
Example 56
Customer Billing System
The print routine of the customer billing
accepts customer data structure as an
argument, parses it, and prints the name,
address, and billing information.
Improvement?
56