Posted by : Sushanth Thursday 17 December 2015

Coupling and Cohesion:
---------------------
Coupling defines the degree to which each component depends on other components in the system. Given two components A and B ,how much code in B must change if A changes.
Cohesion defines the measure of how coherent or strongly related the various functions of a single software component are.It refers to what the class does.
Low cohesion would mean that the class does a great variety of actions and is not focused on what it should do. High cohesion would then mean that the class is focused on what it should be doing, i.e. only methods relating to the intention of the class.
Note: Good APIs exhibit loose coupling and high cohesion.
One particularly abhorrent form of tight coupling that should always be avoided is having two components that depend on each other directly or indirectly, that is, a dependency cycle or circular dependency.
Ways to reduce the coupling:
----------------------------
1.Use a forward declaration for a class unless you actually need to #include its full definition.
If class A only needs to know the name of class B, that is, it does not need to know the size of class B or call any methods in the class, then class A does not need to depend on the full declaration of B. In these cases, you can use a forward declaration for class B, rather than including the entire interface, and so reduce the coupling between the two classes.
Example:
class Sample; // only need to know the name of Sample
class SampleHolder
{
public:
SampleHolder();
void SetObject(Sample *obj);
Sample *GetObject() const;
private:
Sample *mObj;
};
2.Prefer declaring a function as a non-member non-friend function rather than as a member function. Doing so improves encapsulation and also reduces the degree of coupling of those functions to the class.
3.Manager Classes
A manager class is one that owns and coordinates several lower-level classes. This can be used to break the dependency of one or more classes upon a collection of low-level classes. For example, consider a structured drawing program that lets you create 2D objects, select objects, and move them around a canvas. The program supports several kinds of input devices to let users select and move objects, such as a mouse, tablet, and joystick.
Alternatively, you could introduce a manager class to coordinate access to each of the specific input device classes. In this way, the SelectObject and MoveObject classes only need to depend on this single manager class, and then only the manager class needs to depend on the individual input device classes. This may also require creating some form of abstraction for the underlying classes. For example, note that MouseInput, TabletInput, and JoystickInput each have a slightly different interface. Our manager class could therefore put in place a generic input device interface that abstracts away the specifics of a particular device. The improved, more loosely coupled, design is shown in Figure 2.6.
4.Callbacks, Observers, and Notifications:
Callbacks
In C/C++, a callback is a pointer to a function within module A that is passed to module B so that B can invoke the function in A at an appropriate time. Module B knows nothing about module A and has no include or link dependencies upon A. This makes callbacks particularly useful to allow lowlevel code to execute high-level code that it cannot have a link dependency on. As such, callbacks are a popular technique to break cyclic dependencies in large projects.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Technical Articles - Skyblue - Powered by Blogger - Designed by Johanes Djogan -