耦合性是用来描述模块之间的独立程度。这里讲模块之间的耦合程度分为六个程度R1.-R6(Ri >Rj, i > j)。分别是无耦合,数据耦合,标记耦合,控制耦合,共同耦合,内容耦合。
Ø 无耦合R0: 模块X与模块Y没有交互
Ø 数据耦合R1:模块X与模块Y之间有调用关系,传递的是简单的数据值,相当于高级语言的值传递。一个模块访问另一个模块时,彼此之间是通过简单数据参数 (不是控制参数、公共数据结构或外部变量) 来交换输入、输出信息的。
代码模拟:
package metrics.coupling.data;
import metrics.coupling.data.Person;
public class Main {
public static void main(String[] args) {
Person person = new Person(1.8, 56);
double height = person.getHeight();
double weight = person.getWeight();
<span style="background-color: rgb(51, 204, 0);">double BMI = BMIUtils.getBMI(height, weight);</span>
System.out.println("BMI:" + BMI);
}
}
package metrics.coupling.data;
public class Person {
private double height;
private double weight;
public Person(double height, double weight) {
super();
this.height = height;
this.weight = weight;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
package metrics.coupling.data;
public class BMIUtils {
public static double getBMI(double height, double weight) {
return weight / (Math.pow(height, 2));
}
}
Ø 标记耦合R2:模块X与模块Y之间传递的是数据结构(复合数据类型),一般是指传的是改数据的地址。
代码模拟:
package metrics.coupling.stamp;
import metrics.coupling.stamp.BMIUtils;
import metrics.coupling.stamp.Person;
public class Main {
public static void main(String[] args) {
Person person = new Person(1.8, 56);
<span style="background-color: rgb(51, 204, 0);">double BMI = BMIUtils.getBMI(person);</span>
System.out.println("BMI:" + BMI);
}
}
package metrics.coupling.stamp;
public class Person {
private double height;
private double weight;
public Person(double height, double weight) {
super();
this.height = height;
this.weight = weight;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
package metrics.coupling.stamp;
public class BMIUtils {
public static double getBMI(Person person) {
return person.getWeight() / (Math.pow(person.getHeight(), 2));
}
}
Ø 控制耦合R3:模块X将参数传递给模块Y用以控制模块Y的行为
代码模拟:
package metrics.coupling.control;
public class Main {
public static void main(String[] args) {
Person person = new Person("man");
if(<span style="background-color: rgb(51, 204, 0);">"man".equals(person.getSex())</span>){
System.out.println("是一个男生");
} else if(<span style="background-color: rgb(51, 204, 0);">"woman".equals(person.getSex())</span>){
System.out.println("是一个女生");
} else{
System.out.println("没准是泰国来的");
}
}
}
package metrics.coupling.control;
public class Person {
private String sex;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String sex) {
super();
this.sex = sex;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
Ø 共同耦合R4:模块X与模块Y共享相同的全局变量。
代码模拟:
package metrics.coupling.common;
public class Main {
<span style="background-color: rgb(51, 204, 0);">static StringBuffer mBuffer;</span>
public static void main(String[] args) {
mBuffer = new StringBuffer("我是一个字符串 ");
<span style="background-color: rgb(51, 204, 0);">StringOP1.operate(mBuffer);</span>
<span style="background-color: rgb(51, 204, 0);">StringOP2.operate(mBuffer);</span>
System.out.println(mBuffer.toString());
}
}
package metrics.coupling.common;
public class StringOP1 {
public static void operate(StringBuffer mBuffer) {
mBuffer.append("第一次修改;");
}
}
package metrics.coupling.common;
public class StringOP2 {
public static void operate(StringBuffer mBuffer) {
mBuffer.append("第一次修改;");
}
}
Ø 内容耦合R5:模块X直接修改或操作模块Y的数据,或者直接转入模块Y。
代码模拟:
package metrics.coupling.content;
public class Main {
static StringBuffer mBuffer;
public static void main(String[] args) {
mBuffer = new StringBuffer("我是一个字符串 ");
StringOP1.operate(mBuffer);
//StringOP2.operate(mBuffer);
System.out.println(mBuffer.toString());
}
}
package metrics.coupling.content;
public class StringOP1 {
public static void operate(StringBuffer mBuffer) {
mBuffer.append("第一次修改;");
<span style="background-color: rgb(51, 204, 0);">StringOP2.operate(mBuffer);</span>
}
}
package metrics.coupling.content;
public class StringOP2 {
public static void operate(StringBuffer mBuffer) {
mBuffer.append("第一次修改;");
}
}