通过Java程序来反映各种类型的耦合、内聚-金初阳

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

} 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("man".equals(person.getSex())){ System.out.println("是一个男生"); } else if("woman".equals(person.getSex())){ 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) {
int[] rgb = new int[3]; File file = new File(args); BufferedImage bi = null; try { bi = ImageIO.read(file); } catch (Exception e) { e.printStackTrace(); } int width = bi.getWidth(); int height = bi.getHeight(); int minx = bi.getMinX(); int miny = bi.getMinY(); String[][] list = new String[width][height]; for (int i = minx; i < width; i++) { for (int j = miny; j < height; j++) { int pixel = bi.getRGB(i, j); rgb[0] = (pixel & 0xff0000) >> 16; rgb[1] = (pixel & 0xff00) >> 8; rgb[2] = (pixel & 0xff); list[i][j] = rgb[0] + "," + rgb[1] + "," + rgb[2]; } } return list; } public static void compareImage(String imgPath1, String imgPath2){ String[] images = {imgPath1, imgPath2}; if (images.length == 0) { System.out.println("Usage >java BMPLoader ImageFile.bmp"); System.exit(0); } // 分析图片相似度 begin String[][] list1 = getPX(images[0]); String[][] list2 = getPX(images[1]); int xiangsi = 0; int busi = 0; int i = 0, j = 0; for (String[] strings : list1) { if ((i + 1) == list1.length) { continue; } for (int m=0; m<strings.length; m++) { try { String[] value1 = list1[i][j].toString().split(","); String[] value2 = list2[i][j].toString().split(","); int k = 0; for (int n=0; n<value2.length; n++) {
import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; /** * 比较两张图片的相似度 * @author Guihua * */ public class BMPLoader { // 改变成二进制码 public static String[][] getPX(String args) {
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 { 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.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。 代码模拟:
通过 Java 程序来反映各种类型的耦合、内聚
耦合性是用来描述模块之间的独立程度。这里讲模块之间的耦合程度分为六个程度 R0-R5 (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(); double BMI = BMIUtils.getBMI(height, weight); 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 wHale Waihona Puke Baiduight) { 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); double BMI = BMIUtils.getBMI(person); 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;
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("第一次修改;"); StringOP2.operate(mBuffer); } } package metrics.coupling.content; public class StringOP2 { public static void operate(StringBuffer mBuffer) { mBuffer.append("第一次修改;"); } } 内聚:内聚是将为了完成相同的任务的范围的程度。分为功能内聚,顺序内聚,通信内聚,过 程内聚,时间内聚,耦合内聚。 功能内聚:模块只执行一个单一定义良好的功能。
相关文档
最新文档