L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling...

25
L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter coupling Subclass coupling Cohesion Coincidental cohesion Logical cohesion Temporal cohesion Communication cohesion Sequential cohesion Functional cohesion Data cohesion Visibility: visibility & access and friends Chapter 23 of Budd

Transcript of L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling...

Page 1: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

L14: Coupling, Cohesion, Visibility• Dependency

• Coupling

Internal data coupling

Global data coupling

Control (or sequence) coupling

Component coupling

Parameter coupling

Subclass coupling

• Cohesion

Coincidental cohesion

Logical cohesion

Temporal cohesion

Communication cohesion

Sequential cohesion

Functional cohesion

Data cohesion

• Visibility: visibility & access and friends

• Chapter 23 of Budd

Page 2: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Dependency

• If an object cannot meaningfully exist without another object, it is said to be dependent on the second object.

• Eg a child class is almost always dependent on its parent.

Page 3: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Coupling

• Strength of interaction between objects. Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter coupling Subclass coupling

Page 4: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Internal Data Coupling

class Sneaky {

public:

void sneak(){

luke->father=“darth”;

};

private:

Luke *luke;

};

class Luke {

public:

Luke() {

father = “anakin”;

};

string father;

};

Page 5: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Global Data Coupling

• Two or more classes are bound together by their reliance on common global data structures

double todaysDow;

class One {public: void setDow(){

todaysDow=10534; }};

class Two {public: void printDow(){ cout << todaysDow; }};

Page 6: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Global Data Coupling

• File scope: names are defined outside blocks or classes.• Namespace scope: names are defined within a namespace

block.• Function scope: labels are the only names that have function

scope. They can be used anywhere within a function, but are not accessible outside that function.

• Class scope : names of class members have class scope.• Prototype scope: names declared in a function prototype are

visible only until the end of the prototype.

Page 7: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Control Coupling• When one class must perform operations in a fixed order, but the

order is controlled elsewhere

class MyClass {public:

void doFirst(){...};void doSecond(){...};

void do(int option) { switch(option) { case 1: doFirst(); break; case 2: doSecond(); break;

default: break; } }}

Page 8: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Component Coupling

• One class maintains a data field or value which is an instance of another class.

• Ideally this relationship should be one way.

class Set {...

private:List data;

};

Page 9: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Parameter Coupling

• When one class must invoke routines from another via parameters.

class MyClass {public: void doSomething(Set aSet){

... }};

Page 10: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Subclass Coupling

• Describes the relationship between a class and its parent.

class Parent {...};

class Child: public Parent {...};

Page 11: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Rules to Reduce Coupling

• Always access data members through accessor methods. E.g., getFather, setFather.

• Advantages?• Within a class method only access:

– arguments– instance variables– local variables

• Try to reduce the number of classes each class knows about.

• Use namespaces for global data (scope reduction).

Page 12: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Cohesion• Degree to which the tasks performed by a single

module are functionally related Coincidental cohesion Logical cohesion Temporal cohesion Communicational cohesion Sequential cohesion Functional cohesion Data cohesion

Page 13: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Coincidental Cohesion

Elements of a class are grouped for no apparent reason.

A class consists of methods that are not related. Usually a sign of poor design.

Page 14: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Logical Cohesion

Occurs when there is logical connection among the elements of the class but no actual connection in either data or control.

E.g., a library of mathematical functions

Page 15: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Temporal Cohesion

Elements are bound together because they all must be used at approximately the same time.

E.g., a class that performs program initialisation.

Page 16: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Communicational cohesion

Methods are grouped together because they all access the same input/output data or device.

The class acts as a device manager. E.g., a Proxy class

Page 17: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Sequential Cohesion

• Elements are linked by the necessity to be activated in a particular order.

• Often used to attempt to avoid sequential coupling.

Page 18: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Functional Cohesion

A highly cohesive function: a function only performs one task.

Very desirable form of cohesion, and highly reusable.

Page 19: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Data Cohesion

A class defines a set of data values and exports routines that manipulate the data structure.

The embodiment of an ADT E.g., vector, list etc

Page 20: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Visibility

• An object is visible in a certain context if its name is legal and denotes the object.

• I.e., the object is in scope.

Page 21: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Access and Visibility

class Sneaky {

private:

int safe;

public:

Sneaky(){safe=10;};

int &sorry(){return safe;};

print(){cout << safe << endl;}

};

Sneaky x;

x.sorry() = 1;

x.print(); // safe=1

int y = x.sorry();

y = 2;

x.print(); // safe=1

int &z = x.sorry();

z = 3;

x.print(); // safe=3

Page 22: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Friends

• C++ has a notion of friend functions and classes.

• A friend function is a function that can access a classes private parts.

• Can be useful but easily abused!

Page 23: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Example I: Function as Friend

class Vector; // Forward declaration of class Vector

class Matrix {float data[4][4];

public:friend Vector operator*(const Matrix&, const Vector&);

};class Vector {

float data[4];public:

friend Vector operator*(const Matrix&, const Vector&);}

Vector operator*(const Matrix& m, const Vector& v);

Page 24: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

Example II: Class as Friend

class X; // Forward declaration of class X

class F {

public:

void f_print(X& x);

};

class X {

int a, b;

friend class F;

public:

X() : a(1), b(2) { }

};

void F::f_print(X& x) {

cout << "a is " << x.a << endl;

cout << "b is " << x.b << endl;

}

int main() {

X xobj;

F fobj;

fobj.f_print(xobj);

}

Page 25: L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter.

References Farrell, J. (2009) Object-Oriented Programming Using C++.

4th ed.

MSDN (2009). Visual C++ Language Reference: Scope. http://msdn.microsoft.com/en-us/library/b7kfh662(VS.80).aspx