You are on page 1of 31

Software Design and

Architecture

Fakhar Lodhi
Topics Covered: 016 – 020
Desirable Internal Design Characteristics
Software Design and
Architecture
Topic#016
What is good design?
What is good design?
· Is it efficient code?
· compact implementation?
· Most maintainable?
Maintainable Design
· Cost of system changes is minimal
· Readily adaptable to modify existing functionality and
enhance functionality
· Design is understandable
· Changes should be local in effect

High Cohesion and Low Coupling


Software Design and
Architecture
Topic#016

END!
Software Design and
Architecture
Topic#017
Coupling
Coupling
· Coupling is the measure of dependency between
modules/components.
· The degree of interdependence between two
modules/components
· A dependency exists between two
modules/components if a change in one could
require a change in the other.

Inter-component relationship
Measuring Coupling
· The degree of coupling between modules is
determined by:
· The number of interfaces between modules (quantity)
· Complexity of each interface (determined by the type of
communication) (quality)
Why is high degree of coupling
bad?
· Highly coupled systems have strong interconnection
· High dependence of program units upon one another
· Difficult to test, reuse, and understand separately

Loosely coupled modules are


less dependent upon each other
and hence are easier to change
We aim to minimize coupling to
make modules as independent
as possible!
Coupling – How to reduce it?
· Low coupling can be achieve by:
· eliminating unnecessary relationships
· reducing the number of necessary relationships
· easing the ‘tightness’ of necessary relationships
Software Design and
Architecture
Topic#017

END!
Software Design and
Architecture
Topic#018
Coupling – Example
Design of a simple web app
<!doctype html>
<html>
<head>
<script type="text/javascript" src="base.js">
</script>
<link rel="stylesheet" href="default.css">
</head>
<body>
<button onclick="highlight2()">Highlight </button>
<button onclick="normal2()">Normal </button>
<h1 id="title" class="NormalClass">
CSS <--> JavaScript Coupling
</h1>
</body>
</html>
.NormalClass {
color:inherit;
font-style:normal;
}

default.css
<!doctype html>
<html>
<head>
<script type="text/javascript" src="base.js">
</script>
<link rel="stylesheet" href="default.css">
</head>
<body>
<button onclick="highlight2()">Highlight </button>
<button onclick="normal2()">Normal </button>
<h1 id="title" class="NormalClass">
CSS <--> JavaScript Coupling
</h1>
</body> We want to change the style of the title in
</html>
response to user action
function highlight() {
document.getElementById("title").
style.color="red";
document.getElementById("title").
style.fontStyle="italic";
}

function normal() {
document.getElementById("title").
style.color="inherit";
document.getElementById("title").
style.fontStyle="normal";
}
Option A
JavaScript code modifies the style attribute of HTML element.
function highlight() {
document.getElementById("title").
className = "HighlightClass";
}

function normal() {
document.getElementById("title").
className = "NormalClass";
}

Option B
JavaScript code modifies the class attribute of HTML element.
.NormalClass {
color:inherit;
font-style:normal;
}

.HighlightClass {
color:red;
font-style:italic;
}

default.css
Software Design and
Architecture
Topic#018

END!
Software Design and
Architecture
Topic#019
Cohesion
Cohesion
· Cohesion refers to the degree to which the
elements inside a module belong together
· A measure of the strength of relationship between:
· the methods and data of a class and some unifying
purpose or concept served by that class
· the class’s methods and data themselves

A module has high cohesion if all of its


elements are working towards the same goal
Cohesion: Intra-component relationship

Low Cohesion High Cohesion


High vs Low Cohesion
Highly Cohesive Modules Lowly Cohesive Modules
· Easier to understand · Difficult to understand
· More Robust · Difficult to maintain
· More Reliable · Difficult to test
· More Reusable · Difficult to reuse
Software Design and
Architecture
Topic#019

END!
Software Design and
Architecture
Topic#020
Relationship between Coupling
and Cohesion
High Cohesion and Low Coupling -
Benefits
Highly Cohesive Modules Lowly Coupled Modules
· easier to read and · easier to read and
understand understand
· Easier to modify · Easier to modify
· Increased potential for · Increased potential for
reuse reuse
· Easier to develop and test · Easier to develop and test

Surprised?
Are they somehow related?
Low Cohesion and High Coupling High Cohesion and Low Coupling
Cohesion and Coupling
· Coupling: Inter-component relationships
· Cohesion: Intra-component relationships

The best designs have


high cohesion within a module and
low coupling between modules
Cohesion and Coupling
Tend to be Inversely Correlated

When Coupling is high, cohesion tends to be


low and vise versa
Software Design and
Architecture
Topic#020

END!

You might also like