Coupling

Types of Modular Coupling

In order of desirability

  • Data Coupling (weakest ­ most desirable)
  • Control Coupling
  • Global Data Coupling
  • Internal Data Coupling (strongest ­ least desirable)
  • Content Coupling (Unrated)

Data Coupling

Output from one module is the input to another

Using parameter lists to pass items between routines

Object A passes object X to object B

Object X and B are coupled

A change to X's interface may require a change to B

 

class Receiver {

   public void message( MyType X ) {

          // code goes here      

         X.doSomethingForMe( Object data );

         // more code

    }

}

 

Control Coupling

Passing control flags between modules so that one module controls the sequencing of the processing steps in another module

A sends a message to B

B uses a parameter of the message to decide what to do

class Lamp {

       public static final ON = 0;

       public void setLamp( int setting ) {
             if ( setting == ON ) //turn light on
            else if ( setting == 1 ) // turn light off
            else if ( setting == 2 ) // blink }
}

 

Lamp reading = new Lamp();

reading.setLamp( Lamp.ON );

reading.setLamp( 2 );

Problem: A needs to know the messages it can pass to B.

A Solution:

Decompose the operation into multiple primitive operations

class Lamp {
       public void on() {//turn light on }
       public void off() {//turn light off }
       public void blink() {//blink }

}

Lamp reading = new Lamp();

reading.on();

reading.blink();

 

Global Data Coupling

Two or more modules share the same global data structures

Example

A method in one object makes a specific reference to a specific external object

Internal Data Coupling

One module directly modifies local data of another module

Example: C++ Friends

Lexical Content Coupling

Some or all of the contents of one module are included in the contents of another

Example: C/C++ header files

Solution: Restrict what goes in header file

© Lynne Grewe