耦合性是用来描述模块之间的独立程度。这里讲模块之间的耦合程度分为六个程度
无耦合R0:模块
数据耦合)来交换输入、输出信息的。
代码模拟:
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));}}
标记耦合(复合数据类型),一般是指传的是改数据的地址。
代码模拟:
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));}}
控制耦合
代码模拟:
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:
代码模拟:
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:
代码模拟:
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("第一次修改;");}}
,在繁华中体会热闹;若是厌倦了喧嚣,寻一处宁静的幽谷,