Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter."— Presentation transcript:

1 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

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

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

4 Internal Data Coupling class Sneaky { public: void sneak(){ luke->father=darth; }; private: Luke *luke; }; class Luke { public: Luke() { father = anakin; }; string father; };

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

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

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

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

9 Parameter Coupling When one class must invoke routines from another via parameters. class MyClass { public: void doSomething(Set aSet){... } };

10 Subclass Coupling Describes the relationship between a class and its parent. class Parent {...}; class Child: public Parent {...};

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

12 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

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.

14 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

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

16 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

17 Sequential Cohesion Elements are linked by the necessity to be activated in a particular order. Often used to attempt to avoid sequential coupling.

18 Functional Cohesion A highly cohesive function: a function only performs one task. Very desirable form of cohesion, and highly reusable.

19 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

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.

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(); // safe=1 int y = x.sorry(); y = 2; x.print(); // safe=1 int &z = x.sorry(); z = 3; x.print(); // safe=3

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!

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

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

25 References Farrell, J. (2009) Object-Oriented Programming Using C++. 4 th ed. MSDN (2009). Visual C++ Language Reference: Scope.


Download ppt "L14: Coupling, Cohesion, Visibility Dependency Coupling Internal data coupling Global data coupling Control (or sequence) coupling Component coupling Parameter."

Similar presentations


Ads by Google