You are on page 1of 13

10-Design Concepts 11/11/19

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

1.1. Design levels Key design concepts

Architectures/Framework General OO Specific


(Financial System, J2EE,…)

OOD Patterns • Cohesion • Behaviors follow data


• Coupling • Class vs. Interface
OOD Principles Inheritance
• Information hiding • Class = implementation
Specific Data Structures
• Encapsulation • Interface = type
Algorithmic Approaches
• Creation • Inheritance /
General + OO Concepts
• Binding time composition /
delegation
7

3 4

@Nguyễn Thị Thu Trang, trangntt@soict.hust.edu.vn 1


10-Design Concepts 11/11/19

5 6

Modules Ideals of modular software


• A module is a relatively general term for a class or a type • Decomposable – can be broken down into
modules to reduce complexity and allow
or any kind of design unit in software
teamwork
• A modular design focuses on what modules are defined, • Composable – “Having divided to conquer, we
what their specifications are, how they relate to each must reunite to rule [M. Jackson].”
other, but not usually on the implementation of the • Understandable – one module can be
modules themselves examined, reasoned about, developed, etc. in
isolation
• Continuity – a small change in the
• Overall, you’ve been given the modular design so far –
requirements should affect a small number of
and now you have to learn more about how to do the modules
design • Isolation – an error in one module should be
as contained as possible

5 6

1.2. Good Design Two general design issues


• What’s a design? • Cohesion – why are sub-modules (like methods)
• Express a idea to resolve a problem placed in the same module? Usually to
collectively form an ADT
• Use for communications in the team members
• Coupling – what is the dependence between
• What’s a good design? modules? Reducing the dependences (which
• Easy for Developing, Reading & Understanding come in many forms) is desirable
• Easy for Communication
• Easy for Extending (add new features)
• Easy for Maintenance

7 8

@Nguyễn Thị Thu Trang, trangntt@soict.hust.edu.vn 2


10-Design Concepts 11/11/19

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

Cohesion and Coupling Good design


q Coupling
• Easy for Developing, Reading &
Coupling or Dependency is the degree to which each program
module relies on each one of the other modules. Understanding
• Easy for Communication
class Coupling

Service A Service B what's happen when changing


Service A --> Service B?

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

@Nguyễn Thị Thu Trang, trangntt@soict.hust.edu.vn 3


10-Design Concepts 11/11/19

Coupling: Degree of dependence among


Content components
1. How do you design?
2. Coupling
3. Cohesion No dependencies Loosely coupled-some dependencies

High coupling makes modifying


parts of the system difficult, e.g.,
Highly coupled-many dependencies modifying a component affects all
the components to which the
component is connected.

13 14

Content
Common

Range of Coupling 2.1. Content coupling


Control
Stamp
Data
Uncoupled

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

@Nguyễn Thị Thu Trang, trangntt@soict.hust.edu.vn 4


10-Design Concepts 11/11/19

Content
Common

Exercise of Content Coupling 2.2. Common Coupling Control


Stamp
Data
Uncoupled

• Definition: Two components share data


Part of program handles lookup for customer. • Global data structures
When customer not found, component adds • Common blocks
customer by directly modifying the contents of • Usually a poor design choice because
the data structure containing customer data
• Lack of clear responsibility for the data
• Reduces readability
=> How to improve? • Difficult to determine all the components that affect a
data element (reduces maintainability)
• Difficult to reuse components
• Reduces ability to control data accesses

17 18

Exercise of Common Coupling 2.3. Control Coupling


• Process control component maintains • Definition: Component passes control
current data about state of operation. Gets parameters to coupled components.
data from multiple sources. Supplies data • May be either good or bad, depending on
to multiple sinks. situation.
• Each source process writes directly to • Bad when component must be aware of internal
global data store. Each sink process reads structure and logic of another module
directly from global data store. • Good if parameters allow factoring and reuse of
=> How to improve? functionality

19 20

@Nguyễn Thị Thu Trang, trangntt@soict.hust.edu.vn 5


10-Design Concepts 11/11/19

22

Example 1 Exercise 2 – Control Coupling


• In your video store, you might eventually
• Acceptable: Module p calls module q and
q passes back flag that says it cannot create a method like this:
complete the task, then q is passing data • updateCustomer(int whatKind, Customer
customer) where whatKind takes on the
• Not Acceptable: Module p calls module q values ADD, EDIT or DELETE,
and q passes back flag that says it cannot • and customer is used for EDIT, but is not used
complete the task and, as a result, writes at all for ADD, and only the id is used for
a specific message. DELETE.

21 22

23

Exercise 3 – Control Coupling 2.4. Stamp Coupling


• In your video store, you might eventually • Definition: Component passes a data structure to
create a method like this: another component that does not have access to
the entire structure.
• editCustomer(intwhatKind, Customer
customer) where whatKind takes on the • Requires second component to know how to
values RETAIL, or AGENCY manipulate the data structure (e.g., needs to
know about implementation)
• May be necessary due to efficiency factors: this is
a choice made by insightful designer, not lazy
programmer.

23 24

@Nguyễn Thị Thu Trang, trangntt@soict.hust.edu.vn 6


10-Design Concepts 11/11/19

Example of Stamp Coupling 2.5. Data Coupling


• Definition: Two components are data coupled if
Customer billing system
there are homogeneous data items.
The print routine of the customer billing • Every argument is simple argument or data
accepts a customer data structure as an structure in which all elements are used
argument, parses it, and prints the name, • Good, if it can be achieved.
address, and billing information. • Easy to write contracts for this and modify
component independently.
=> How to improve?

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

@Nguyễn Thị Thu Trang, trangntt@soict.hust.edu.vn 7


10-Design Concepts 11/11/19

Range of Cohesion 3.1. Coincidental Cohesion


• Definition: Parts of the component are only
High Cohesion
Functional related by their location in source code
Informational
• Elements needed to achieve some
Sequential
functionality are scattered throughout the
Communicational system.
Functional
Procedural
• Accidental Informational
Sequential
Temporal
• Worst form
Communicational
Procedural
Temporal
Logical Logical
Coincidental
Coincidental Low

29 30

Example 3.2. Logical Cohesion


• Print next line • Definition: Elements of component are related
logically and not functionally.
• Reverse string of characters in second
• Several logically related elements are in the
argument
same component and one of the elements is
• Add 7 to 5th argument selected by the client component.
• Convert 4th argument to float
Functional
Informational
Sequential
Communicational
Procedural
Temporal
Logical
Coincidental

31 32

@Nguyễn Thị Thu Trang, trangntt@soict.hust.edu.vn 8


10-Design Concepts 11/11/19

Functional
Informational

Example of Logical Cohesion 3.3. Temporal Cohesion Sequential


Communicational
Procedural
Temporal

• A component reads inputs from tape, • Definition: Elements of a component


Logical
Coincidental

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

Example of Temporal Cohesion 3.4. Procedural Cohesion


• A system initialization routine: this routine • Definition: Elements of a component are
contains all of the code for initializing all related only to ensure a particular order of
of the parts of the system. Lots of execution.
different activities occur, all at init time. • Actions are still weakly connected and
unlikely to be reusable
• => How to improve? Functional
Informational
Sequential
Communicational
Procedural
Temporal
Logical
Coincidental

35 36

@Nguyễn Thị Thu Trang, trangntt@soict.hust.edu.vn 9


10-Design Concepts 11/11/19

Example 3.5. Communicational Cohesion


... • May be useful to • Definition: Module performs a series of
Read part number from abstract the intent of
this sequence. Make
actions related by a sequence of steps to
database the data base and be followed by the product and all actions
update repair record on repair record are performed on the same data
maintenance file. components handle
... reading and updating.
Make component that Functional
Informational
handles more abstract Sequential

operation. Communicational
Procedural
Temporal
Logical
Coincidental

37 38

Example 3.6. Sequential Cohesion


• Update record in data • database.Update • The output of one component is the input to
base and send it to the (record). another.
printer. • record.Print().
• Occurs naturally in functional programming
languages
• Good situation
Functional
Informational
Sequential
Communicational
Procedural
Temporal
Logical
Coincidental

39 40

@Nguyễn Thị Thu Trang, trangntt@soict.hust.edu.vn 10


10-Design Concepts 11/11/19

Functional
Informational

3.7. Informational Cohesion Sequential


Communicational
Procedural
3.8. Functional Cohesion
Temporal

• Definition: Every essential element to a


Logical
• Definition: Module performs a number of Coincidental

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

Examples of Cohesion Examples of Cohesion-2

Function A Function A Time t0 Function A Function A


Function Function
B C logic Function A’ Time t0 + X
Function B Function B
Function Function Time t0 + 2X
D E Function A’’ Function C Function C
Coincidental Logical Temporal
Parts unrelated Communicational Sequential
Similar functions Related by time Access same data Output of one is input to another

Function A Function A part 1

Function B Function A part 2

Function C Function A part 3

Procedural Functional
Related by order of functions Sequential with complete, related functions

43 44

@Nguyễn Thị Thu Trang, trangntt@soict.hust.edu.vn 11


10-Design Concepts 11/11/19

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

An object should only send messages to …


(More Demeter) Coupling is the path to the dark side
Guidelines: not strict rules! • Coupling leads to complexity
• itself (this)
But thinking about them • Complexity leads to confusion
• its instance variables will generally help you • Confusion leads to suffering
• its method's parameters produce better designs
• any object it creates
• Once you start down the dark path,
• any object returned by a call to one of this's methods forever will it dominate your destiny,
• any objects in a collection of the above consume you it will

• notably absent: objects returned by messages sent to


other objects

47 48

@Nguyễn Thị Thu Trang, trangntt@soict.hust.edu.vn 12


10-Design Concepts 11/11/19

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)

• A god class is an example of an anti-pattern – it is a


known bad way of doing things

49

@Nguyễn Thị Thu Trang, trangntt@soict.hust.edu.vn 13

You might also like