设计模式六大原则(5):迪米特法则

转自
[url]http://blog.csdn.net/zhengzhb/article/details/7296930[/url]

[b]定义:[/b]一个对象应该对其他对象保持最少的了解。
[b]问题由来:[/b]类与类之间的关系越密切,耦合度越大,当一个类发生改变时,对另一个类的影响也越大。
[b]解决方案:[/b]尽量降低类与类之间的耦合。
自从我们接触编程开始,就知道了软件编程的总的原则:低耦合,高内聚。无论是面向过程编程还是面向对象编程,只有使各个模块之间的耦合尽量的低,才能提高代码的复用率。低耦合的优点不言而喻,但是怎么样编程才能做到低耦合呢?那正是迪米特法则要去完成的。
迪米特法则又叫最少知道原则,最早是在1987年由美国Northeastern University的Ian Holland提出。通俗的来讲,就是一个类对自己依赖的类知道的越少越好。也就是说,对于被依赖的类来说,无论逻辑多么复杂,都尽量地的将逻辑封装在类的内部,对外除了提供的public方法,不对外泄漏任何信息。[b][color=red]迪米特法则还有一个更简单的定义:只与直接的朋友通信[/color][/b]。首先来解释一下什么是直接的朋友:每个对象都会与其他对象有耦合关系,只要两个对象之间有耦合关系,我们就说这两个对象之间是朋友关系。耦合的方式很多,依赖、关联、组合、聚合等。其中,我们称出现成员变量、方法参数、方法返回值中的类为[color=red][b]直接的朋友[/b][/color],而出现在局部变量中的类则不是直接的朋友。也就是说,陌生的类最好不要作为局部变量的形式出现在类的内部。
举一个例子:有一个集团公司,下属单位有分公司和直属部门,现在要求打印出所有下属单位的员工ID。先来看一下违反迪米特法则的设计。

#include <iostream>
#include <sstream>
#include <string>
#include <list>
using namespace std;

//总公司员工
class Employee
{
public:
void SetId(string strId)
{
this->m_strId = strId;
}

string GetId()
{
return m_strId;
}

private:
string m_strId;

};

//分公司员工
class SubEmployee
{
public:
void SetId(string strId)
{
this->m_strId = strId;
}

string GetId()
{
return m_strId;
}

private:
string m_strId;
};

class SubCompanyManager
{
public:
list<SubEmployee> GetAllEmployee()
{
list<SubEmployee> tempList;
for(int i=0; i<100; i++)
{
ostringstream strStream;
SubEmployee Temp;
//为分公司人员按顺序分配一个ID
strStream << "分公司 " << i ;
Temp.SetId(strStream.str());
tempList.push_back(Temp);
}

return tempList;
}
};

class CompanyManager
{
public:
list<Employee> GetAllEmployee()
{
list<Employee> tempList;
for(int i = 0; i < 30; i++)
{
Employee Temp;
ostringstream strStream;
//为总公司人员按顺序分配一个ID
strStream << "总公司 " << i ;
Temp.SetId(strStream.str());
tempList.push_back(Temp);
}

return tempList;
}

void PrintAllEmployee(SubCompanyManager& sub)
{
list<SubEmployee> list1 = sub.GetAllEmployee();
list<SubEmployee>::iterator it;
for(it = list1.begin(); it != list1.end(); it++)
{
cout << it->GetId() << "\n";
}

list<Employee> list2 = GetAllEmployee();
list<Employee>::iterator it2;
for(it2 = list2.begin(); it2 != list2.end(); it2++)
{
cout << it2->GetId() << "\n";
}
}
};
void main()
{
CompanyManager e;
SubCompanyManager sub;
e.PrintAllEmployee(sub);
}


分公司 0
......
分公司 99
总公司 0
......
总公司 29

现在这个设计的主要问题出在CompanyManager中,根据迪米特法则,只与直接的朋友发生通信,而SubEmployee类并不是CompanyManager类的直接朋友(以局部变量出现的耦合不属于直接朋友),从逻辑上讲总公司只与他的分公司耦合就行了,与分公司的员工并没有任何联系,这样设计显然是增加了不必要的耦合。按照迪米特法则,应该避免类中出现这样非直接朋友关系的耦合。修改后的代码如下:

#include <iostream>
#include <sstream>
#include <string>
#include <list>
using namespace std;

//总公司员工
class Employee
{
public:
void SetId(string strId)
{
this->m_strId = strId;
}

string GetId()
{
return m_strId;
}

private:
string m_strId;

};

//分公司员工
class SubEmployee
{
public:
void SetId(string strId)
{
this->m_strId = strId;
}

string GetId()
{
return m_strId;
}

private:
string m_strId;
};

class SubCompanyManager
{
public:
list<SubEmployee> GetAllEmployee()
{
list<SubEmployee> tempList;
for(int i=0; i<100; i++)
{
ostringstream strStream;
SubEmployee Temp;
//为分公司人员按顺序分配一个ID
strStream << "分公司 " << i ;
Temp.SetId(strStream.str());
tempList.push_back(Temp);
}

return tempList;
}

void PrintEmployee()
{
list<SubEmployee> list2 = GetAllEmployee();
list<SubEmployee>::iterator it2;
for(it2 = list2.begin(); it2 != list2.end(); it2++)
{
cout << it2->GetId() << "\n";
}
}
};

class CompanyManager
{
public:
list<Employee> GetAllEmployee()
{
list<Employee> tempList;
for(int i = 0; i < 30; i++)
{
Employee Temp;
ostringstream strStream;
//为总公司人员按顺序分配一个ID
strStream << "总公司 " << i ;
Temp.SetId(strStream.str());
tempList.push_back(Temp);
}

return tempList;
}

void PrintAllEmployee(SubCompanyManager& sub)
{
sub.PrintEmployee(); //分离

list<Employee> list2 = GetAllEmployee();
list<Employee>::iterator it2;
for(it2 = list2.begin(); it2 != list2.end(); it2++)
{
cout << it2->GetId() << "\n";
}
}
};
void main()
{
CompanyManager e;
SubCompanyManager sub;
e.PrintAllEmployee(sub);
}


分公司 0
......
分公司 99
总公司 0
......
总公司 29

修改后,[b][color=red]为分公司增加了打印人员ID的方法,总公司直接调用来打印,从而避免了与分公司的员工发生耦合。这样的话,如果分公司打印方法或则格式有动的话,只需要修改分公司的代码即可,在不修改打印接口的情况下,不需要修改总公司的代码。[/color][/b]
迪米特法则的初衷是降低类之间的耦合,由于每个类都减少了不必要的依赖,因此的确可以降低耦合关系。但是凡事都有度,虽然可以避免与非直接的类通信,但是要通信,必然会通过一个“中介”来发生联系,例如本例中,总公司就是通过分公司这个“中介”来与分公司的员工发生联系的。过分的使用迪米特原则,会产生大量这样的中介和传递类,导致系统复杂度变大。所以在采用迪米特法则时要反复权衡,既做到结构清晰,又要高内聚低耦合。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值