CSC/ECE 517 Summer 2008/wiki2 6 cc

From Expertiza_Wiki
Revision as of 18:51, 24 June 2008 by Smrober4 (talk | contribs)
Jump to navigation Jump to search

Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.


Types of Cohesion

Functional Cohesion

Functional cohesion describes a module that is designed to perform one and only one task. A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task. The following example illustrates functional cohesion.

 public class Stack 
 {
   public Stack()   
   {
     // implementation
   }
   
   public void push(Object obj)
   {
     // implementation
   }
   
   public Object pop()
   {
     // implementation
   }
   
   public int getSize()
   {
     // implementation
   }
 }
  
Sequential Cohesion

Sequential cohesion describes modules whose operations are intended to executed in sequence with the output of each operation providing input to the subsequently executed operation. The following example illustrates sequential cohesion [1].

 public class PayCheckCalculator()
 { 
   public float CalculateBenefitsDeducations(float payAmount)
   {
     // implementation details
   }
   
   public float CalculateFedDeducations(float payAmount)
   {
     // implementation details
   }
   
   public float CalculateStateDeductions(float payAmount)
   {
     // implementation details
   }
   
   public float CalculateRetirementDeductions(float payAmount)
   {
     // implementation details
   }
 }
Communicational Cohesion

Communicational cohesion describes modules that perform multiple operations on the same input or output data [1]. The following example illustrates communication cohesion [2].

 public class CustomerInformation
 {
   public CustomerInformation(int accountNum)
   {
     // implementation details
   }
   
   public String getName()
   {
     // implementation details
   }
   
   public float getBalance()
   {
     // implementation details
   }
   
   // ...
 }
Procedural Cohesion

Procedural cohesion is similar to sequential cohesion in that the operations exposed are typically grouped because they are executed within a sequence. Unlike sequential cohesion however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily use as input to a following operation. The following example illustrates procedural cohesion.

 public class Student
 {
   public Student(int studentId)
   {
     // implementation details
   }
   
   public void LoadStudent()
   {
     // implementation details
   }
   
   public void UpdateGrades(int[] grades)
   {
     // implementation details
   }
   
   public void UpdateAttendance(Date[] dates)
   {
     // implementation details
   }
   
   public void SaveStudent()
   {
     // implementation details
   }
 }
Temporal Cohesion

Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity. The following example illustrates temporal cohesion.

 public class Startup
 {
   public void InitializeLogging()
   {
     // implementation details
   }
   
   public void InitializeUI()
   {
     // implementation details
   }
   
   public void InitializeDb()
   {
     // implementation details
   }
 }
Logical Cohesion

Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different. Typically, these modules accept a control flag which indicates which operation to execute. The following example illustrates logical cohesion [2].

 public class DataStore
 {
   public void SaveData(int destination, byte[] data)
   {
     switch (destination)
     {
       default:
       case 0:
         SaveToDb(data)
         break;
       
       case 1:
         SaveToFile(data)
         break;
       
       case 2:
         SaveToWebService(data)
         break;
     }
   }
   
   protected void SaveToDb(byte[] data)
   {
     // implementation details
   }
   
   protected void SaveToFile(byte[] data)
   {
     // implementation details
   }
   
   protected void SaveToWebService(byte[] data)
   {
     // implementation details
   }
 }
Coincidental Cohesion

Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks. typically used to accomplish several unrelated tasks. The following example illustrates coincidental cohesion [1].

 public static class Math
 {
   public static int Add(int a, int b)
   {
     // implementation details
   }
   
   public static int Subtract(int a, int b)
   {
     // implementation details
   }
   
   // ...
 }

References

  1. http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069
  2. http://www.waysys.com/ws_content_bl_pgssd_ch06.html
  3. http://www.site.uottawa.ca:4321/oose/index.html#cohesion
  4. http://javaboutique.internet.com/tutorials/coupcoh/index-2.html
  5. http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm