1 课程讲解
1.1 定义
1.2 特质
1.3 重点
2 代码演练
2.1 反例
2.2 正例
1 课程讲解
1.1 定义
定义:一个对象应该对其他对象保持最少的了解。又叫最少知道原则。
1.2 特质
核心:降低类的耦合
优点:降低类的耦合
1.3 重点
只和朋友交流,不和陌生人说话
朋友:出现在成员变量、方法的输入、输出参数中的类称为成员朋友类,而出现在方法体内部的类不属于朋友类。
2 代码演练
2.1 反例
测试类:
package com.geely.design.principle.demeter; public class TestDemeter { public static void main(String [] args){ //testNegated BossNegated bossNegated = new BossNegated(); bossNegated.commandTecherCountCours(); //testPositive //BossPositive bossPositive = new BossPositive(); //bossPositive.commandTecherCountCours(); } }
测试结果:
书的总数量为:20
Process finished with exit code 0
老板类:
package com.geely.design.principle.demeter; import java.util.ArrayList; import java.util.List; /** * 此处设计不合理,只访问朋友类(成员变量中的类,输入中出现的类,输出中出现的类) * 成员方法中的类不需要引入(CourseNegated) */ public class BossNegated { public void commandTecherCountCours(){ TecherNegated teacher = new TecherNegated(); List<CourseNegated> list = new ArrayList<CourseNegated>(); for(int i= 0;i<20;i++){ list.add(new CourseNegated()); } teacher.countCourse(list); } }
老师类:
package com.geely.design.principle.demeter; import java.util.List; public class TecherNegated { public void countCourse(List list){ System.out.println("书的总数量为:"+list.size()); } }
课程类:
package com.geely.design.principle.demeter; /** */ public class CourseNegated { }
2.2 正例
测试类:
package com.geely.design.principle.demeter; public class TestDemeter { public static void main(String [] args){ //testNegated //BossNegated bossNegated = new BossNegated(); //bossNegated.commandTecherCountCours(); //testPositive BossPositive bossPositive = new BossPositive(); bossPositive.commandTecherCountCours(); } }
测试结果:
"C:\Program Files\Java\jdk1.6.0_43\bin\java.exe" "-javaagent:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar=5695:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.6.0_43\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\jce.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\resources.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\rt.jar;F:\xiangmu3\Xin\Idea\out\production\design_pattern" com.geely.design.principle.demeter.TestDemeter
书的总数量为:20
Process finished with exit code 0
老板类:
package com.geely.design.principle.demeter; /** * */ public class BossPositive { public void commandTecherCountCours(){ TecherPositive teacher = new TecherPositive(); teacher.countCourse(); } }
老师类:
package com.geely.design.principle.demeter; import java.util.ArrayList; import java.util.List; public class TecherPositive { public void countCourse(){ List<CourseNegated> list = new ArrayList<CourseNegated>(); for(int i= 0;i<20;i++){ list.add(new CourseNegated()); } System.out.println("书的总数量为:"+list.size()); } }
课程类:
package com.geely.design.principle.demeter; /** */ public class CourseNegated { }
诸葛