Coupling Indicate the relationship between the different modules. Cohesion indicates a relation inside of a single module.
Cohesion
- Cohesion is the indication of the relationship within module.
- Cohesion shows the module’s relative functional strength.
- Cohesion is a degree (quality) to which a component / module focuses on thesingle thing.
- While designing you should strive for high cohesion i.e. a cohesive component/ module focus on a single task (i.e., single-mindedness) with little interaction with other modules of the system.
- Cohesion is the kind of natural extension of data hiding for example, classhaving all members visible with a package having default visibility.
- Cohesion is Intra – Module Concept.
Coupling
- Coupling is the indication of the relationships between modules.
- Coupling shows the relative independence among the modules.
- Coupling is a degree to which a component / module is connected to the other modules.
- While designing you should strive for low coupling i.e. dependency between modules should be less.
- Making private fields, private methods and non public classes provides loose coupling.
- Coupling is Inter -Module Concept.
Types of Coupling
module here refers to a subroutine of any kind, i.e. a set of one or more statements having a name and preferably its own set of variable names.
- Content coupling (high)
- Content coupling (also known as Pathological coupling) occurs when one module modifies or relies on the internal workings of another module (e.g., accessing local data of another module). In this situation, a change in the way the second module produces data (location, type, timing) might also require a change in the dependent module.
- Common coupling
- Common coupling (also known as Global coupling) occurs when two modules share the same global data (e.g., a global variable). Changing the shared resource might imply changing all the modules using it.
- int global=10;void fun1(){global++;printf("\ni am Updating Global Data");}void fun2(){global++;printf("\ni am also Updating Global Data");}void main(){fun1();fun2();}
- External coupling
- External coupling occurs when two modules share an externally imposed data format, communication protocol, or device interface. This is basically related to the communication to external tools and devices.
- extern external=10;void fun1(){external++;printf("\ni am External Global Data");}
- Control coupling
- Control coupling is one module controlling the flow of another, by passing it information on what to do (e.g., passing a what-to-do flag).
- int findflag(int s,int ar[3]){int i;for(i=0;i<3;i++){if(ar[i]==s)break;}if(i==3)return 0; /* flag zero indicate value not found */elsereturn 1; /* flag one indicate value found */}void search(int search,int ar[3]){if(findflag(search,ar)==1)printf("Found");elseprintf("Not Found");}void main(){int ar[]={1,2,3};search(2,ar);}
- Stamp coupling (Data-structured coupling)
- Stamp coupling occurs when modules share a composite data structure and use only parts of it, possibly different parts (e.g., passing a whole record to a function that only needs one field of it).
- In this situation, a modification in a field that a module does not need may lead to changing the way the module reads the record.
- struct student{int roll;char name[10];char city[10];};void show(struct student s){printf("\nRoll=%d",s.roll);printf("\nName=%s",s.name);printf("\nCity=%d",s.city);}void main(){struct student rec={101,"sona","Vns"};show(rec);
} - Data coupling
- Data coupling occurs when modules share data through, for example, parameters. Each datum is an elementary piece, and these are the only data shared (e.g., passing an integer to a function that computes a square root).
- int sqrt(int n){return n*n;}void main(){printf("SQRT of 2 is %d",sqrt(2));
} - Message coupling (low)
- This is the loosest type of coupling. It can be achieved by state decentralization (as in objects) and component communication is done via parameters or message passing.
- No coupling
- Modules do not communicate at all with one another.
No comments:
Post a Comment