l14: coupling, cohesion, visibility dependency coupling internal data coupling global data coupling...

Post on 26-Mar-2015

272 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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.

Coupling

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

Internal Data Coupling

class Sneaky {

public:

void sneak(){

luke->father=“darth”;

};

private:

Luke *luke;

};

class Luke {

public:

Luke() {

father = “anakin”;

};

string father;

};

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; }};

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.

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; } }}

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;

};

Parameter Coupling

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

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

... }};

Subclass Coupling

• Describes the relationship between a class and its parent.

class Parent {...};

class Child: public Parent {...};

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).

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

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.

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

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.

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

Sequential Cohesion

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

• Often used to attempt to avoid sequential coupling.

Functional Cohesion

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

Very desirable form of cohesion, and highly reusable.

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

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.

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

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!

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);

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);

}

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

top related