l11: coupling, cohesion, visibility -...
TRANSCRIPT
L11: 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
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 1
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.
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 2
Coupling
• Internal data coupling• Global data coupling• Control (or sequence) coupling• Component coupling• Parameter coupling• Subclass coupling
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 3
Internal Data Coupling
class Sneaky {public:
void sneak(){ luke->father=“darth”;};
private:Luke *luke;
};
class Luke {public:
Luke() { father = “anakin”;};string father;
};
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 4
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; }};
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 5
Global Data Coupling
• File scope• Namespace scope• Program scope
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 6
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 doThird(){...};
}
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 7
Component Coupling
• One class maintains a data field or value that is an instance of another class.
• Ideally this relationship should be one way.
class Set {...
private:List data;
};
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 8
Parameter Coupling
• When one class must invoke routines from another via parameters.
class MyClass {public: void doSomething(Set aSet){
... }};
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 9
Subclass Coupling
• Describes the relationship between a class and its parent.
class Parent {...};
class Child: public Parent {...};
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 10
Rules to Reduce Coupling
• Always access data members through accessor methods. Eg getFather, setFather.
• Advantages?
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 11
Rules to Reduce Coupling
• 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.
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 12
Cohesion
• Coincidental cohesion• Logical cohesion• Temporal cohesion• Communication cohesion• Sequential cohesion• Functional cohesion• Data cohesion
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 13
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.
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 14
Logical Cohesion
• Occurs when there is logical connection among the elements of the class but no actual connection in either data or control.
• Eg a library of mathematical functions
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 15
Temporal Cohesion
• Elements are bound together because they all must be used at approximately the same time.
• Eg a class that performs program initialisation.
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 16
Communications cohesion
• Methods are grouped together because they all access the same input/output data or device.
• The class acts as a device manager.• Eg a Proxy class
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 17
Sequential Cohesion
• Elements are linked by the necessity to be activated in a particular order.
• Often used to attempt to avoid sequential coupling.
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 18
Function Cohesion
• The elements of a class all relate to the performance of a single function.
• Very desirable form of cohesion
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 19
Data Cohesion
• A class defines a set of data values and exports routines that manipulate the data structure.
• The embodiment of an ADT• Eg vector, list etc
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 20
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.
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 21
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();int y = x.sorry(); y = 2;x.print();int &z = x.sorry();z = 3;x.print();
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 22
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!
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 23
Example
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);
SFDV4001 / L11 - Coupling, Cohesion & Visibility / 24