Professional Documents
Culture Documents
Content
1. How do you design?
SOFTWARE DESIGN AND CONSTRUCTION
2. Coupling
10. DESIGN CONCEPTS
3. Cohesion
Nguyen Thi Thu Trang
trangntt@soict.hust.edu.vn
1 2
3 4
5 6
5 6
7 8
9 10
Roughly, the more coupled k modules are,
the more one needs to think of them as a
Cohesion Coupling single, larger module
• Themost common reason to put elements – data • How are modules dependent on one another?
and behavior – together is to form an ADT • Statically (in the code)? Dynamically (at run-time)? And
• Sometimes may be other reasons, e.g. performance reasons: more
place together all code to be run upon initialization of a • Ideally, split design into parts that don't interact much
program
MY MY
• The common design objective of separation of MY
concerns suggests a module should address a FINAL
PROJECT
single set of concerns FINAL PROJECT FINAL PROJECT
• Should Item/DiscountItem know about added discount for A poor decomposition A better decomposition
purchasing 20+ items? An application
(parts strongly coupled) (parts weakly coupled)
• Should ShoppingCart know about bulk pricing?
• An artist’s rendition – to really assess coupling one needs
• Should BinarySearch know the type of the objects it is sorting?
to know what the arrows are, etc.
9 10
Product A
• Easy for Extending (add new features)
• Easy for Maintenance
Application «interface»
IProduct
Product B
Cohesion
Loosely coupled
q
Cohesion refers to the degree to which the elements of a è “Loose coupling and high cohesion”
module belong together. Cohesion is a measure of how strongly-
related or focused the responsibilities of a single module are.
11 12
13 14
Content
Common
High Coupling
Content • Definition: One component references
Common
contents of another
Control • Example:
• Component directly modifies another’s data
Loose
Stamp
• Component refers to local data of another
Data component in terms of numerical displacement
Uncoupled • Component modifies another’s code, e.g.,
Low jumps into the middle of a routine
15 16
Content
Common
17 18
19 20
22
21 22
23
23 24
25 26
Content 3. Cohesion
• Definition: The degree to which all elements of a
1. How do you design?
component are directed towards a single task
2. Coupling and all elements directed towards that task are
contained in a single component.
3. Cohesion • Internal glue with which component is
constructed
• All elements of component are directed toward
and essential for performing the same task
• High is good
27 28
29 30
31 32
Functional
Informational
disk, and network. All the code for are related by timing.
these functions are in the same • Difficult to change because you may have
component. to look at numerous components when a
• Operations are related, but the change in a data structure is made.
functions are significantly different. • Increases chances of regression fault
• Component unlikely to be reusable.
• => How to improve?
33 34
35 36
operation. Communicational
Procedural
Temporal
Logical
Coincidental
37 38
39 40
Functional
Informational
actions, each with its own entry point, with single computation is contained in the
independent code for each action, all performed component.
on the same data.
• Every element in the component is
• Different from logical cohesion essential to the computation.
• Each piece of code has single entry and single exit
• In logical cohesion, actions of module intertwined
• Ideal situation. Functional
Informational
Sequential
• ADT and object-oriented paradigm promote Communicational
Procedural
Temporal
Logical
Coincidental
41 42
Procedural Functional
Related by order of functions Sequential with complete, related functions
43 44
45 46
Law of Demeter
Different kinds of dependences Karl Lieberherr i and colleagues
• Aggregation – “is part of” is a field that is a sub-part • Law of Demeter: An object should know as little as possible
• Ex: A car has an engine about the internal structure of other objects with which it
• Composition – “is entirely made of” has the parts live and interacts – a question of coupling
die with the whole • Or… “only talk to your immediate friends”
• Ex: A book has pages (but perhaps the book cannot exist without • Closely related to representation exposure and
the pages, and the pages cannot exist without the book) (im)mutability
• Subtyping – “is-a” is for substitutability
• Bad example – too-tight chain of coupling between classes
• Invokes – “executes” is for having a computation general.getColonel().getMajor(m).getCaptain(cap)
performed .getSergeant(ser).getPrivate(name).digFoxHole();
• In other words, there are lots of different kinds of arrows • Better example
(dependences) and clarifying them is crucial general.superviseFoxHole(m, cap, ser, name);
45 46
47 48
47 48
49
God classes
• God class: a class that hoards too much of the data or
functionality of a system
• Poor cohesion – little thought about why all of the elements are
placed together
• Only reduces coupling by collapsing multiple modules into one (and
thus reducing the dependences between the modules to
dependences within a module)
49