Coupling
 

Back To Design Concepts

Coupling: Coupling is a property of a collection of modules. Coupling is the measure of interdependence between modules. Coupling is an indication of how modules are connected by data.

Example: Two modules can share the same pieces of data, or one module can pass its data into the other module. In general, we try to reduce coupling - we try to make sure our modules do not depend on each other.

Low coupling: A module with a low degree of interdependence from other modules is said to show low or loose coupling. Loose coupling is more desirable, because a particular module is not tied to a program, and it can easily be placed in other programs without the programmer having to worry about the data being used.

High coupling: A module with a high degree of dependence on other modules is said to show high or tight coupling. If two modules are tightly coupled, it would be difficult to re-use one of them, because in the program you place one of the modules, it would have to use the same data. 


Example:
Lets look at an example, so we can understand this concept better. Below is a program based on a game that children often play: A player picks a color, type of car, and a number, and another player uses this information to make up a "fortune" for the first player. Trace the code below:



start
declare variables ----[ char color
[ char car
[ num a Number
get Inputs
display Fortune
end

get Inputs()
print "Enter your favorite color."
get color
print "Enter your dream car."
get car
print "Enter your favorite number."
get a Number
return

display Fortune()
print "You are going to live in a "
print color, " house, drive a "
print car, " and have ", number, " kids!
return


How re-usable are the modules in this program? If we tried to use the display Fortune() module in another program, we would have to make sure that this program used global variables called "color", "car", and "a Number". This is not a very re-usable program! Also, what if you wanted to add or change one of the variables? You would have to make the addition or change in more than one location in this program, plus make the same change in all the other programs where you use this module! This is a good example of high coupling.


Different kinds of coupling are:
1.Data coupling.
2.Content coupling.
3.Common coupling.
4.Control coupling.
5.Stamp coupling.

                                                                                          back to top

Data coupling: Two modules are data coupled if they communicate by passing parameters, each parameter being an elementary piece of data. This is the best form of coupling.

Example: 

                                                                                          back to top

Content coupling: Two modules are content coupled if one refers to the inside of the other in any way: 
  A module refers to or changes data in another.
  A module alters a statement in another. 
  A module branches into another.
  This is the worst type of coupling.


                                                                                          back to top

Common coupling: When two or more modules use the same globally-declared data-structures such as an array or a record structure. Common coupling refers to the use of global system area, either in RAM or in the disk. This implies that one module writes in an area of the disk and the other module reads from that area.
This form of coupling is avoided. 

 back to top

Control coupling: A form of coupling in which control flags are passed between modules so that one module controls the sequence of execution in another module. This type of coupling is typically used when a module does a number of function and only one of them has to be done at a time.

Example 1:

Type of Record flag:
1 means get next master record
2 means get next transaction record
3 means get both



Example 2:
Suppose we have written a common module which adds, modifies, deletes, prints, and displays records from a file. We can only do one of the above five activities at a given time. From the calling module, a flag will have to be passed to indicate the type of activity to be done. This logic can be applied to an Interest computation module, which calculates simple and compound interest. A flag could be passed to indicate the type of interest to be computed.

back to top

Stamp coupling: This type of coupling is similar to data coupling, the main difference being that group data is passed across modules in stamp coupling while elementary data was passed in data coupling.

Example 1:

 

 

back to top
Back To Design Concepts

Hosted by www.Geocities.ws

1