- Coupling indicates the degree of dependence among components. Higher coupling tends to indicate poor design of classes, since it makes modifying parts of the system difficult, as modifying a component affects all the components to which the component is connected.
- Class A has behaviourA and propertiesA, and Class B has behaviourB and propertiesB. Class A uses the properties and behaviours in Class B, and Class B also uses the properties and behaviours in Class A.
- We call High Coupling (Tighly Coupling)
What is the solution ?
- Lower Coupling is better .
Problem: Sample code High Coupling
package com.scjp6.oop.coupling;
public class CalculateTaxes {
float rate;
float doIndia() {
TaxRatesInIndia str = new TaxRatesInIndia();
return rate = str.salesRate; // ouch again
}
}
public class CalculateTaxes {
float rate;
float doIndia() {
TaxRatesInIndia str = new TaxRatesInIndia();
return rate = str.salesRate; // ouch again
}
}
package com.scjp6.oop.coupling;
public class TaxRatesInIndia {
public float salesRate; // should be private
public float adjustedSalesRate; // should be private
public float getSalesTaxRates() {
adjustedSalesRate = new CalculateTaxes().doIndia();
return adjustedSalesRate;
}
}
package com.scjp6.oop.coupling;
public class LowCoupling {
public static void main(String[] args) {
// TODO Auto-generated method stub
Calculate calculate = new CalculateTaxeRateIndiaImpl();
System.out.println(calculate.doIndia());
System.out.println(calculate.getSalesTaxRates());
}
}
Solution: Sample code Low/ Tigh Couplingpublic class LowCoupling {
public static void main(String[] args) {
// TODO Auto-generated method stub
Calculate calculate = new CalculateTaxeRateIndiaImpl();
System.out.println(calculate.doIndia());
System.out.println(calculate.getSalesTaxRates());
}
}
package com.scjp6.oop.coupling;
public interface Calculate {
float doIndia();
float getSalesTaxRates();
}
package com.scjp6.oop.coupling;
public class CalculateTaxeRateIndiaImpl implements Calculate {
@Override
public float doIndia() {
return 0;
}
@Override
public float getSalesTaxRates() {
return 0;
}
}
package com.scjp6.oop.coupling;
public class LowCoupling {
public static void main(String[] args) {
Calculate calculate = new CalculateTaxeRateIndiaImpl();
System.out.println(calculate.doIndia());
System.out.println(calculate.getSalesTaxRates());
}
}
What is Cohesion
- Cohesion is the extent to which methods in a class are related. It means, a class has many methods are related.
- We can call Low Cohesion
What is the solution ?
- High Cohesion is better .
Problem: Sample code Low Cohesion
package com.scjp6.oop.cohesion;
public class ReportGrade {
Student getStudent() {
return new Student();
}
Subject getSubject() {
return new Subject();
}
Grade getGrade(){
return new Grade();
}
}
public class ReportGrade {
Student getStudent() {
return new Student();
}
Subject getSubject() {
return new Subject();
}
Grade getGrade(){
return new Grade();
}
}
package com.scjp6.oop.cohesion;
public class RunLowerCohesion {
public static void main(String[] args) {
ReportGrade reportGrade = new ReportGrade();
reportGrade.getGrade();
reportGrade.getStudent();
reportGrade.getSubject();
}
}
Solution: Sample code High Cohesion
public class Grade {
String studentID;
String subjectID;
float grade01;
float grade02;
}
String studentID;
String subjectID;
float grade01;
float grade02;
}
package com.scjp6.oop.cohesion;
public class Student {
String studentID;
String name;
int age;
}
package com.scjp6.oop.cohesion;
public class Subject {
String subjectID;
String subjectName;
}
package com.scjp6.oop.cohesion;
public class ReportGradeHighCohesion {
Grade getGrade(){
return new Grade();
}
}
package com.scjp6.oop.cohesion;
public class ReportStudentHighCohesion {
Student getStudent() {
return new Student();
}
}
package com.scjp6.oop.cohesion;
public class ReportSubjectHighCohesion {
Subject getSubject() {
return new Subject();
}
}
package com.scjp6.oop.cohesion;
public class RunHighCohesion {
public static void main(String[] args) {
ReportGradeHighCohesion reportGradeHighCohesion = new ReportGradeHighCohesion();
reportGradeHighCohesion.getGrade();
ReportStudentHighCohesion reportStudentHighCohesion = new ReportStudentHighCohesion();
reportStudentHighCohesion.getStudent();
ReportSubjectHighCohesion reportSubjectHighCohesion = new ReportSubjectHighCohesion();
reportSubjectHighCohesion.getSubject();
}
}
public static void main(String[] args) {
ReportGradeHighCohesion reportGradeHighCohesion = new ReportGradeHighCohesion();
reportGradeHighCohesion.getGrade();
ReportStudentHighCohesion reportStudentHighCohesion = new ReportStudentHighCohesion();
reportStudentHighCohesion.getStudent();
ReportSubjectHighCohesion reportSubjectHighCohesion = new ReportSubjectHighCohesion();
reportSubjectHighCohesion.getSubject();
}
}