SlideShare une entreprise Scribd logo
1  sur  20
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

Contenu connexe

Tendances

(研究会輪読) Facial Landmark Detection by Deep Multi-task Learning
(研究会輪読) Facial Landmark Detection by Deep Multi-task Learning(研究会輪読) Facial Landmark Detection by Deep Multi-task Learning
(研究会輪読) Facial Landmark Detection by Deep Multi-task Learning
Masahiro Suzuki
 
Pavel_Kravchenko_Mobile Development
Pavel_Kravchenko_Mobile DevelopmentPavel_Kravchenko_Mobile Development
Pavel_Kravchenko_Mobile Development
Ciklum
 

Tendances (20)

Structure & Union in C++
Structure & Union in C++Structure & Union in C++
Structure & Union in C++
 
27702788
2770278827702788
27702788
 
Dimensionality reduction
Dimensionality reductionDimensionality reduction
Dimensionality reduction
 
Lecture18 structurein c.ppt
Lecture18 structurein c.pptLecture18 structurein c.ppt
Lecture18 structurein c.ppt
 
Structure c
Structure cStructure c
Structure c
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Structure in c sharp
Structure in c sharpStructure in c sharp
Structure in c sharp
 
Kandemir Inferring Object Relevance From Gaze In Dynamic Scenes
Kandemir Inferring Object Relevance From Gaze In Dynamic ScenesKandemir Inferring Object Relevance From Gaze In Dynamic Scenes
Kandemir Inferring Object Relevance From Gaze In Dynamic Scenes
 
Functional Domain Modeling
Functional Domain ModelingFunctional Domain Modeling
Functional Domain Modeling
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
Java 7
Java 7Java 7
Java 7
 
PCA and SVD in brief
PCA and SVD in briefPCA and SVD in brief
PCA and SVD in brief
 
Structures
StructuresStructures
Structures
 
Partition Sort Revisited: Reconfirming the Robustness in Average Case and Muc...
Partition Sort Revisited: Reconfirming the Robustness in Average Case and Muc...Partition Sort Revisited: Reconfirming the Robustness in Average Case and Muc...
Partition Sort Revisited: Reconfirming the Robustness in Average Case and Muc...
 
Structures
StructuresStructures
Structures
 
Structures in c programming
Structures in c programmingStructures in c programming
Structures in c programming
 
(研究会輪読) Facial Landmark Detection by Deep Multi-task Learning
(研究会輪読) Facial Landmark Detection by Deep Multi-task Learning(研究会輪読) Facial Landmark Detection by Deep Multi-task Learning
(研究会輪読) Facial Landmark Detection by Deep Multi-task Learning
 
Optimising Data Using K-Means Clustering Algorithm
Optimising Data Using K-Means Clustering AlgorithmOptimising Data Using K-Means Clustering Algorithm
Optimising Data Using K-Means Clustering Algorithm
 
Pavel_Kravchenko_Mobile Development
Pavel_Kravchenko_Mobile DevelopmentPavel_Kravchenko_Mobile Development
Pavel_Kravchenko_Mobile Development
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 

En vedette (19)

Lecture 04
Lecture 04Lecture 04
Lecture 04
 
Lecture 06
Lecture 06Lecture 06
Lecture 06
 
Lecture 01
Lecture 01Lecture 01
Lecture 01
 
Lecture 05
Lecture 05Lecture 05
Lecture 05
 
Lecture 03
Lecture 03Lecture 03
Lecture 03
 
Lecture 07
Lecture 07Lecture 07
Lecture 07
 
Lecture 09
Lecture 09Lecture 09
Lecture 09
 
Lecture 08
Lecture 08Lecture 08
Lecture 08
 
Nlp
NlpNlp
Nlp
 
Lecture 02
Lecture 02Lecture 02
Lecture 02
 
Lecture 13
Lecture 13Lecture 13
Lecture 13
 
Lecture 11
Lecture 11Lecture 11
Lecture 11
 
Lecture 15
Lecture 15Lecture 15
Lecture 15
 
Lecture 10
Lecture 10Lecture 10
Lecture 10
 
Lecture 14
Lecture 14Lecture 14
Lecture 14
 
Software Engineering - Lecture 02
Software Engineering - Lecture 02Software Engineering - Lecture 02
Software Engineering - Lecture 02
 
Software engineering lecture notes
Software engineering   lecture notesSoftware engineering   lecture notes
Software engineering lecture notes
 
CSC426 - Software Engineering Lecture Note
CSC426   - Software Engineering Lecture NoteCSC426   - Software Engineering Lecture Note
CSC426 - Software Engineering Lecture Note
 
Software engineering lecture notes
Software engineering lecture notesSoftware engineering lecture notes
Software engineering lecture notes
 

Similaire à Lecture 12

MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
Aravind NC
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
karymadelaneyrenne19
 
2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++
Jeff TUYISHIME
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
rashmita_mishra
 

Similaire à Lecture 12 (20)

Object Oriented Dbms
Object Oriented DbmsObject Oriented Dbms
Object Oriented Dbms
 
Advance oops concepts
Advance oops conceptsAdvance oops concepts
Advance oops concepts
 
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
MC0083 – Object Oriented Analysis &. Design using UML - Master of Computer Sc...
 
Interview preparation for programming.pptx
Interview preparation for programming.pptxInterview preparation for programming.pptx
Interview preparation for programming.pptx
 
Java chapter 3
Java   chapter 3Java   chapter 3
Java chapter 3
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Ch14
Ch14Ch14
Ch14
 
Data structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdData structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pd
 
Mca 504 dotnet_unit3
Mca 504 dotnet_unit3Mca 504 dotnet_unit3
Mca 504 dotnet_unit3
 
C++
C++C++
C++
 
Uml - An Overview
Uml - An OverviewUml - An Overview
Uml - An Overview
 
PATTERNS09 - Generics in .NET and Java
PATTERNS09 - Generics in .NET and JavaPATTERNS09 - Generics in .NET and Java
PATTERNS09 - Generics in .NET and Java
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
 
software_engg-chap-03.ppt
software_engg-chap-03.pptsoftware_engg-chap-03.ppt
software_engg-chap-03.ppt
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
 
2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
Kripanshu MOOC PPT - Kripanshu Shekhar Jha (1).pptx
Kripanshu MOOC PPT - Kripanshu Shekhar Jha (1).pptxKripanshu MOOC PPT - Kripanshu Shekhar Jha (1).pptx
Kripanshu MOOC PPT - Kripanshu Shekhar Jha (1).pptx
 
Module 4.pptx
Module 4.pptxModule 4.pptx
Module 4.pptx
 
Euclideus_Language
Euclideus_LanguageEuclideus_Language
Euclideus_Language
 

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