设计模式之6大原则(5)-迪米特法则

设计模式学习笔记——迪米特法则

这里写图片描述

本文转载自:http://blog.csdn.net/hackerain/article/details/7526404
参考: http://blog.csdn.net/jaikydota163/article/details/52764487?locationNum=1&fps=1
http://blog.csdn.net/lovelion/article/details/7563445

迪米特法则是针对类之间的低耦合而提出的,它要求一个类应该对自己需要耦合或者调用的类知道的最少,即被耦合或调用的类的内部如何复杂,跟本类没有一点关系,本类就知道你提供给我多少public方法,本类就调用这些方法,其他的你不要让我知道。
这个法则有以下基层含义:
1、只和朋友类交流
什么是朋友类呢?即和自己相耦合的类。既然相耦合,那么肯定是这两个耦合的类之间要存在交互,那些不存在交互的,却耦合到了一起,这就不叫做朋友类,这也是设计的缺陷,不交互却耦合到了一起,可想而知,这样的设计维护性是很差的,应该尽量避免这样的设计。
2、朋友类间也是有间距的
即相耦合的类之间不能过于亲密,提供给我那么多public方法,让我欢喜让我忧啊。相耦合的类应该彼此知道的最少,用最少的方法提供给我需要的功能就可以了,这样一来,类间的耦合程度就降低了很多,不管是维护起来还是理解起来,都是比较容易的。下面举个例子:
在安装软件的时候,会有一个导向动作,第一步干什么,第二步干什么,其类图如下:
这里写图片描述
代码如下:

public class Wizard {  
    private Random rand=new Random(System.currentTimeMillis());  

    public int first(){  
        System.out.println("第一步...");  
        return rand.nextInt(100);  
    }  

    public int second(){  
        System.out.println("第二步...");  
        return rand.nextInt(100);  
    }  

    public int third(){  
        System.out.println("第三步...");  
        return rand.nextInt(100);  
    }  
}  
public class InstallSoftware {  
    public void installSoftware(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) {  
        InstallSoftware invoker=new InstallSoftware();  
        invoker.installSoftware(new Wizard());  
    }  
}  

我们看一下这样的设计有什么问题,Wizard类暴露给InstallSoftware类太多的方法,在installSoftware()方法中,用了Wizard提供的所有的public方法,这样的耦合度是不是有些太高了?假如说要将返回值从int修改为boolean,那么除了修改Wizard类外,还要大幅度修改InstallSoftware类,这样修改的风险就大大提高了啊,显然这不是一个好的设计,那么我们来看一看应用迪米特法则之后,改进的类图:
这里写图片描述
改进之后的代码:

public class Wizard {  
    private Random rand=new Random(System.currentTimeMillis());  

    private int first(){  
        System.out.println("第一步...");  
        return rand.nextInt(100);  
    }  

    private int second(){  
        System.out.println("第二步...");  
        return rand.nextInt(100);  
    }  

    private int third(){  
        System.out.println("第三步...");  
        return rand.nextInt(100);  
    }  

    public void installWizard(){  
        int first=first();  
        if(first>50){  
            int second=second();  
            if(second>50){  
                int third=third();  
                if(third>50){  
                    first();  
                }  
            }  
        }  
    }  
}  
public class InstallSoftware {  
    public void installSoftware(Wizard wizard){  
        wizard.installWizard();  
    }  
}  

场景类不变,在Wizard类中新增了一个public的installWizard()方法,在本类内部对安装过程进行了封装,并且设置为公开的,而其他的方法都设置为私有的,即这个Wizard类对外只提供了installWizard()这个方法,在InstallSoftware类中,通过Wizard对象只调用这个方法,就可以实现功能,这样一来,类间的耦合度就降低了很多,若是要修改Wizard类中的返回值,影响的也只是Wizard类本身,其他的类不受影响,这就显示了类的高内聚特性。

小结:
一个类公开的public方法或是属性越多,修改时涉及的面也就越大,变更引起的风险扩散也就越大。
迪米特法则的核心就是类间的解耦,弱耦合,只有弱耦合以后,类的复用率才可用提高。

猜你喜欢

转载自blog.csdn.net/linshenggui/article/details/78214833