面向对象五大原则_5.迪米特原则

来源:互联网 发布:手机视频保密软件 编辑:程序博客网 时间:2024/05/16 19:32

朋友类?  

   定义:出现在成员变量,方法输入输出参数中的类,而出现在方法内部的内部不称为朋友类。
  类就是人,人就是类,换个角度,类是实例的接口。 人的关系有朋友,那么类也有朋友。所以我们称之为朋友类,朋友类的关系从耦合,组合,聚合,依赖等衍生出来。

直接的朋友? 调用于你内部是什么样子,我一概不关心。 

I.我只对你说

Only talk to your immedate friends.   只与直接的朋友通信。   

例如,体育老师要体育委员去清点女生的人数。

老师在command中定义出所有女生,然后发布命令给体育委员。代码如下
public class Teacher {  public void command(GroupLeader groupLeader){   List<Girl> list=new ArrayList<Girl>();   for(int i=0;i<20;i++){    list.add(new Girl());   }  groupLeader.countNumbers(list); }}
public class GroupLeader {  public void countNumbers(List<Girl> list){  System.out.println("来了"+list.size()+"个"); }}
public static void main(String[] args) {  Teacher teacher = new Teacher();  teacher.command(new GroupLeader()); }}

关系清理:老师的朋友类是体育委员GroupLeader,体育委员的朋友类是Girl,而老师与Girl有了交流,这样就破坏的Teacher的健壮性。    

public class Teacher {  private GroupLeader groupLeader;  public Teacher(GroupLeader groupLeader){  this.groupLeader=groupLeader; } public void command(){  groupLeader.countNumbers(); }}
public class GroupLeader { private List<Girl> list; public GroupLeader(List<Girl> list){  this.list=list; } public void countNumbers(){  System.out.println("来了"+list.size()+"个"); }}
public static void main(String[] args) {   List<Girl> list=new ArrayList<Girl>();   for(int i=0;i<20;i++){    list.add(new Girl());   }   GroupLeader leader=new GroupLeader(list);   Teacher teacher=new Teacher(leader);   teacher.command(); }}
避开了老师访问学生,降低了系统间的耦合,提高了系统的健壮性。一个类只和朋友交流,不与陌生类交流。


II.我们也有距离

人与人之间是有距离的,如果把握的不好,原本非常要好的兄弟就会变成比一般的朋友关系还差的熟悉人。   
例如下面:
我们在安装软件的时候,经常会有一个导向动作,第一步是确认是否安装,第二步确认License,再然后选择安装目录……这是一个典型的顺序执行动作,具体到程序中就是:调用 一个或多个类,先执行第一个方法,然后是第二个方法,根据返回结果再来看是否可以调用 第三个方法,或者第四个方法,等等


public class Wizard { private Random rand=new Random(System.currentTimeMillis());  public int first(){  System.out.println("---1----");  return rand.nextInt(100); }  public int second(){  System.out.println("---2----");  return rand.nextInt(100); }  public int third(){  System.out.println("---3----");  return rand.nextInt(100); }}
public class InstallSofteare { public void install(Wizard wizard) {  int first = wizard.first();  if (first > 50) {   int second = wizard.second();   if (second > 50) {    int third = wizard.third();    if (third > 50) {     wizard.first();    }   }  } }}
public class Client {  public static void main(String[] args) {  InstallSofteare softeare=new InstallSofteare();  softeare.install(new Wizard()); }}

表面上看起来很好,没啥问题,但是Wizard类把太多的方法暴露给了InstallSofteare 类,关系太密切了。耦合关系就会变得异常牢固,这样风险增加,是极度不合适的。  


public class Wizard { private Random rand=new Random(System.currentTimeMillis());  private int first(){  System.out.println("---1----");  return rand.nextInt(100); }  private int second(){  System.out.println("---2----");  return rand.nextInt(100); }  private int third(){  System.out.println("---3----");  return rand.nextInt(100); }  public void install(){  int first=this.first();  if(first>50){   int second=this.second();   if(second>50){    int third=this.third();    if(third>50){     this.first();    }   }  } }}
public class InstallSofteare { public void install(Wizard wizard){  wizard.install(); }}
public class Client { public static void main(String[] args) {  InstallSofteare softeare = new InstallSofteare();  softeare.install(new Wizard()); }}

 这样看起来风险降低了,耦合关系变弱,结构也清晰,引起的风险也小。  

一个类公开的public属性或方法越多,修改时涉及的面也越大,引起的风险也越大。所以对待朋友要"羞涩",要像男女关系,有所神秘感。

我是菜鸟,我在路上。
0 0
原创粉丝点击