SlideShare a Scribd company logo
Lecture 12

 In this example, we have a class vector in which the data members
have been put in the public part.
 class vector {
public:
float x;
float y;
vector (float x, float y);
float getX();
float getY();
float getMagnitude();
float getAngle();
};
Coupling Example

Coupling Example
 Now let us assume that we want to write a function to
calculate dot product of two vectors. We write the
following function.
 float myDotProduct1(vector a, vector b)
{
float temp1 = a.getX() * b.getX();
float temp2 = a.getY() * b.getY();
return temp1 + temp2;
}

 Since the data members are public, one could be enticed
to use these members directly (presumably saving some
function calls overhead) and rewrite the same function as
follows:
 float myDotProduct2(vector a, vector b)
{
float temp1 = a.x * b.x;
float temp2 = a.y * b.y;
return temp1 + temp2;
}
Coupling Example
 So far, there does not seem to be any issue. But the scenario changes as soon as there
are changes in the class implementation. Now let us assume that for some reason the
class designer changes the implementation and data structure and decides to stores
the angle and magnitude instead of the x and y components of the vector. The new
class looks like as follows:
 class vector {
public:
float magnitude;
float angle;
vector (float x, float y);
vector (float magnitude, float angle);
float getX();
float getY();
float getMagnitude();
float getAngle();
};
Coupling Example

 Now we see the difference in the two
implementations of the dot product function written
by the user of this class. In the first case, as the dot
product function is dependent upon the public
interface of the vector class, there will be no change
while in the second case the function will have to be
rewritten. This is because in the first case the system
was loosely coupled while in the second case there
was more dependency on the internal structure of
the vector class and hence there was more coupling.
Coupling Example
 class book {
Private:
int bookId;
string ISBN;
string author;
string title;
string publisher;
date yearOfPublication;
int borrowerId;
string borrowersName;
string borrowersAddress;
date IssueDate;
date dueDate;
public:
// member functions
};
Cohesion Example

 The Book class shown above represents a book entity
that contains the attributes and behavior of a specific
book information. It is easy to see that this contains
information about the book as well as the borrower
which is a distinct entity. Hence it is not a cohesive
class and must be broken down into two separate
classes.
Cohesion Example

Cohesion vs Coupling

 This diagram depicts two systems, one with high
coupling and the other one with low coupling. The
lines depict linkages between different components.
In the case of highly coupled system, module
boundaries are not well defined, as everything seems
to be connected with everything else. On the other
hand, in the system with low coupling modules can
be identified easily. In this case intra component
linkages are stronger while inter component linkages
are weak.
Cohesion vs Coupling

Cohesion

 As mentioned earlier, strong cohesion implies that
all parts of a component should have a close logical
relationship with each other. That means, in case
some kind of change is required in the software, all
the related pieces are found at one place. A class will
be cohesive if most of the methods defined in a class
use most of the data members most of the time. If we
find different subsets of data within the same class
being manipulated by separate groups of functions
then the class is not cohesive and should be broken
down as shown in the previous slide.
Cohesion

 Abstractions is a technique in which we construct a
model of an entity based upon its essential
characteristics and ignore the inessential details. The
principle of abstraction also helps us in handling the
inherent complexity of a system by allowing us to
look at its important external characteristic, at the
same time, hiding its inner complexity. Hiding the
internal details is called encapsulation.
Abstraction & Encapsulation

 void selectionSort(int a[], int N)
{
int i, j, min, temp;
for (i=0; i < N-1; i++)
{
min = i;
for (j = i; j< N; j++)
if (a[j] < a[min]) min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
Encapsulation Example

 This function can be rewritten by abstracting out some of
the logical steps into auxiliary functions. The new code is
as follows.
 int minimum (int a[], int from, int to)
{
int min = from;
for (int i=from; i <= to; i++)
if (a[i] < a[min]) min = i;
return min;
}
Encapsulation Example

 void selectionSort (int a[], int N)
{
for (i=0; i<N-1; i++)
{
min = minimum (a, i, N-1);
swap (a[i], a[min]);
}
}
Encapsulation Example

 In this function we have abstracted out two logical steps
performed in this functions. These functions are finding
the index of the minimum value in the given range in an
array and swapping the minimum value with the value at
the ith index in the array. It is easy to see that the
resultant new function is easier to understand than the
previous version of the selection sort function. In the
process, as a by-product, we have created two auxiliary
function mentioned above, which are general in nature
and hence can be used elsewhere as well. Principle of
abstraction thus generates reusable self-contained
components.
Encapsulation Example

In the case of function-oriented approach, data is decomposed
according to functionality requirements. That is, decomposition
revolves around function. In the OO approach, decomposition of a
problem revolves around data. Function-oriented paradigm
focuses only on the functionality of a system and typically ignores
the data until it is required. Object- oriented paradigm focuses
both on the functionality and the data at the same time. The basic
difference between these two is decentralized control mechanism
versus centralized control mechanism respectively.
Decentralization gives OO the ability to handle essential
complexity better than function-oriented approach.
Function Oriented vs Object Oriented Design

Function Oriented Design

 In this diagram, the ovals depict the function while
rectangles/squares depict data. Since a function contains
dynamic information while data contains only static
information, if the function and data are managed separately,
the required data components can be found by scanning a
function but the functions that use a particular data cannot be
found by just looking at the data. That is, the function knows
about the data it needs to use but the data do not know about
the functions using it. That means it is easy to make a change in
a function since we would know which data components
would be affected by this change. On the other hand, changing
a data structure would be more difficult because it would not
be easy to find all the functions that are using this data and
hence also need to be modified.
Function Oriented Design

More Related Content

PDF
Structures in c++
PPTX
When to use a structure vs classes in c++
PDF
Modularizing Arcihtectures Using Dendrograms1
PDF
C structure and union
DOCX
8.clustering algorithm.k means.em algorithm
PPTX
Thinking in object oriented - Part 1
PDF
IJERD(www.ijerd.com)International Journal of Engineering Research and Develop...
PDF
Paper id 21201488
Structures in c++
When to use a structure vs classes in c++
Modularizing Arcihtectures Using Dendrograms1
C structure and union
8.clustering algorithm.k means.em algorithm
Thinking in object oriented - Part 1
IJERD(www.ijerd.com)International Journal of Engineering Research and Develop...
Paper id 21201488

What's hot (20)

PPTX
Structure & Union in C++
PDF
Dimensionality reduction
PDF
Lecture18 structurein c.ppt
PPT
Structure c
PPTX
Structure & union
DOCX
Structure in c sharp
PDF
Kandemir Inferring Object Relevance From Gaze In Dynamic Scenes
PDF
Functional Domain Modeling
PPTX
Unit 9. Structure and Unions
PPT
PPTX
PCA and SVD in brief
PPT
Structures
PDF
Partition Sort Revisited: Reconfirming the Robustness in Average Case and Muc...
PDF
Structures
PPTX
Structures in c programming
PDF
(研究会輪読) Facial Landmark Detection by Deep Multi-task Learning
PDF
Optimising Data Using K-Means Clustering Algorithm
PPT
Pavel_Kravchenko_Mobile Development
PPTX
Structure in c language
Structure & Union in C++
Dimensionality reduction
Lecture18 structurein c.ppt
Structure c
Structure & union
Structure in c sharp
Kandemir Inferring Object Relevance From Gaze In Dynamic Scenes
Functional Domain Modeling
Unit 9. Structure and Unions
PCA and SVD in brief
Structures
Partition Sort Revisited: Reconfirming the Robustness in Average Case and Muc...
Structures
Structures in c programming
(研究会輪読) Facial Landmark Detection by Deep Multi-task Learning
Optimising Data Using K-Means Clustering Algorithm
Pavel_Kravchenko_Mobile Development
Structure in c language
Ad

Viewers also liked (19)

PPTX
Lecture 04
PPTX
Lecture 06
PPTX
Lecture 01
PPTX
Lecture 05
PPTX
Lecture 03
PPTX
Lecture 07
PPTX
Lecture 09
PPTX
Lecture 08
PPTX
PPTX
Lecture 02
PPTX
Lecture 13
PPTX
Lecture 11
PPTX
Lecture 15
PPTX
Lecture 10
PPTX
Lecture 14
PPTX
Software Engineering - Lecture 02
PDF
Software engineering lecture notes
PPTX
CSC426 - Software Engineering Lecture Note
PDF
Software engineering lecture notes
Lecture 04
Lecture 06
Lecture 01
Lecture 05
Lecture 03
Lecture 07
Lecture 09
Lecture 08
Lecture 02
Lecture 13
Lecture 11
Lecture 15
Lecture 10
Lecture 14
Software Engineering - Lecture 02
Software engineering lecture notes
CSC426 - Software Engineering Lecture Note
Software engineering lecture notes
Ad

Similar to Lecture 12 (20)

PPT
Object Oriented Dbms
PPTX
Advance oops concepts
DOCX
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
PPTX
Interview preparation for programming.pptx
PDF
Java chapter 3
PDF
Object Oriented Programming notes provided
PPT
Lecture 9
PPT
Ch14
PDF
Data structures and algorithms short note (version 14).pd
DOCX
3rd-Sem_CSE_Data-Structures and Applications.docx
PDF
C++
PPT
Data structure and problem solving ch01.ppt
PPT
Uml - An Overview
PPTX
PATTERNS09 - Generics in .NET and Java
PPTX
Python OOPs
PPT
software_engg-chap-03.ppt
PDF
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
PPT
2 lesson 2 object oriented programming in c++
DOCX
New microsoft office word document (2)
Object Oriented Dbms
Advance oops concepts
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
Interview preparation for programming.pptx
Java chapter 3
Object Oriented Programming notes provided
Lecture 9
Ch14
Data structures and algorithms short note (version 14).pd
3rd-Sem_CSE_Data-Structures and Applications.docx
C++
Data structure and problem solving ch01.ppt
Uml - An Overview
PATTERNS09 - Generics in .NET and Java
Python OOPs
software_engg-chap-03.ppt
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
2 lesson 2 object oriented programming in c++
New microsoft office word document (2)

Lecture 12

  • 2.   In this example, we have a class vector in which the data members have been put in the public part.  class vector { public: float x; float y; vector (float x, float y); float getX(); float getY(); float getMagnitude(); float getAngle(); }; Coupling Example
  • 3.  Coupling Example  Now let us assume that we want to write a function to calculate dot product of two vectors. We write the following function.  float myDotProduct1(vector a, vector b) { float temp1 = a.getX() * b.getX(); float temp2 = a.getY() * b.getY(); return temp1 + temp2; }
  • 4.   Since the data members are public, one could be enticed to use these members directly (presumably saving some function calls overhead) and rewrite the same function as follows:  float myDotProduct2(vector a, vector b) { float temp1 = a.x * b.x; float temp2 = a.y * b.y; return temp1 + temp2; } Coupling Example
  • 5.  So far, there does not seem to be any issue. But the scenario changes as soon as there are changes in the class implementation. Now let us assume that for some reason the class designer changes the implementation and data structure and decides to stores the angle and magnitude instead of the x and y components of the vector. The new class looks like as follows:  class vector { public: float magnitude; float angle; vector (float x, float y); vector (float magnitude, float angle); float getX(); float getY(); float getMagnitude(); float getAngle(); }; Coupling Example
  • 6.   Now we see the difference in the two implementations of the dot product function written by the user of this class. In the first case, as the dot product function is dependent upon the public interface of the vector class, there will be no change while in the second case the function will have to be rewritten. This is because in the first case the system was loosely coupled while in the second case there was more dependency on the internal structure of the vector class and hence there was more coupling. Coupling Example
  • 7.  class book { Private: int bookId; string ISBN; string author; string title; string publisher; date yearOfPublication; int borrowerId; string borrowersName; string borrowersAddress; date IssueDate; date dueDate; public: // member functions }; Cohesion Example
  • 8.   The Book class shown above represents a book entity that contains the attributes and behavior of a specific book information. It is easy to see that this contains information about the book as well as the borrower which is a distinct entity. Hence it is not a cohesive class and must be broken down into two separate classes. Cohesion Example
  • 10.   This diagram depicts two systems, one with high coupling and the other one with low coupling. The lines depict linkages between different components. In the case of highly coupled system, module boundaries are not well defined, as everything seems to be connected with everything else. On the other hand, in the system with low coupling modules can be identified easily. In this case intra component linkages are stronger while inter component linkages are weak. Cohesion vs Coupling
  • 12.   As mentioned earlier, strong cohesion implies that all parts of a component should have a close logical relationship with each other. That means, in case some kind of change is required in the software, all the related pieces are found at one place. A class will be cohesive if most of the methods defined in a class use most of the data members most of the time. If we find different subsets of data within the same class being manipulated by separate groups of functions then the class is not cohesive and should be broken down as shown in the previous slide. Cohesion
  • 13.   Abstractions is a technique in which we construct a model of an entity based upon its essential characteristics and ignore the inessential details. The principle of abstraction also helps us in handling the inherent complexity of a system by allowing us to look at its important external characteristic, at the same time, hiding its inner complexity. Hiding the internal details is called encapsulation. Abstraction & Encapsulation
  • 14.   void selectionSort(int a[], int N) { int i, j, min, temp; for (i=0; i < N-1; i++) { min = i; for (j = i; j< N; j++) if (a[j] < a[min]) min = j; temp = a[i]; a[i] = a[min]; a[min] = temp; } } Encapsulation Example
  • 15.   This function can be rewritten by abstracting out some of the logical steps into auxiliary functions. The new code is as follows.  int minimum (int a[], int from, int to) { int min = from; for (int i=from; i <= to; i++) if (a[i] < a[min]) min = i; return min; } Encapsulation Example
  • 16.   void selectionSort (int a[], int N) { for (i=0; i<N-1; i++) { min = minimum (a, i, N-1); swap (a[i], a[min]); } } Encapsulation Example
  • 17.   In this function we have abstracted out two logical steps performed in this functions. These functions are finding the index of the minimum value in the given range in an array and swapping the minimum value with the value at the ith index in the array. It is easy to see that the resultant new function is easier to understand than the previous version of the selection sort function. In the process, as a by-product, we have created two auxiliary function mentioned above, which are general in nature and hence can be used elsewhere as well. Principle of abstraction thus generates reusable self-contained components. Encapsulation Example
  • 18.  In the case of function-oriented approach, data is decomposed according to functionality requirements. That is, decomposition revolves around function. In the OO approach, decomposition of a problem revolves around data. Function-oriented paradigm focuses only on the functionality of a system and typically ignores the data until it is required. Object- oriented paradigm focuses both on the functionality and the data at the same time. The basic difference between these two is decentralized control mechanism versus centralized control mechanism respectively. Decentralization gives OO the ability to handle essential complexity better than function-oriented approach. Function Oriented vs Object Oriented Design
  • 20.   In this diagram, the ovals depict the function while rectangles/squares depict data. Since a function contains dynamic information while data contains only static information, if the function and data are managed separately, the required data components can be found by scanning a function but the functions that use a particular data cannot be found by just looking at the data. That is, the function knows about the data it needs to use but the data do not know about the functions using it. That means it is easy to make a change in a function since we would know which data components would be affected by this change. On the other hand, changing a data structure would be more difficult because it would not be easy to find all the functions that are using this data and hence also need to be modified. Function Oriented Design