设计模式六大原则是单一职责原则、里氏替换原则、依赖倒置原则、接口隔离原则、迪米特法则、开闭原则。它们不是要我们刻板的遵守,而是根据实际需要灵活运用。只要对它们的遵守程度在一个合理的范围内,努为做到一个良好的设计。本文主要介绍一下.NET(C#) 迪米特法则。
迪米特法则(Law Of Demeter)
迪米特法则(Law of Demeter)又叫作最少知识原则(The Least Knowledge Principle),一个类对于其他类知道的越少越好,就是说一个对象应当对其他对象有尽可能少的了解,只和朋友通信,不和陌生人说话。迪米特法则的初衷在于降低类之间的耦合。由于每个类尽量减少对其他类的依赖,因此,很容易使得系统的功能模块功能独立,相互之间不存在(或很少有)依赖关系。
迪米特法则不希望类之间建立直接的联系。
例如,
1)一般的反面设计实现
using System;
using System.Collections.Generic;
namespace ConsoleApplication
{//学校总部员工类class Employee{public string Id { get; set; }}//学院的员工类class CollegeEmployee{public string Id { get; set; }}//管理学院员工的管理类class CollegeManager{//返回学院的所有员工public List<CollegeEmployee> getAllEmployee(){List<CollegeEmployee> list = new List<CollegeEmployee>();//增加了10个员工到listfor (int i = 0; i < 10; i++){CollegeEmployee emp = new CollegeEmployee();emp.Id="学院员工ID=" + i;list.Add(emp);}return list;}}//学校管理类class SchoolManager{//返回学校总部的员工public List<Employee> getAllEmployee(){List<Employee> list = new List<Employee>();for (int i = 0; i < 5; i++){Employee emp = new Employee();emp.Id = "学校总部员工ID=" + i;list.Add(emp);}return list;}//该方法完成输出学校总部和学院员工信息(ID)public void PrintAllEmployee(CollegeManager sub){//CollegeEmployee不是SchoolManager的直接朋友//CollegeEmployee是以局部变量方式出现在SchoolManager违反了迪米特法则//获取学院员工List<CollegeEmployee> list1 = sub.getAllEmployee();Console.WriteLine("===========学院员工==============");foreach (CollegeEmployee e in list1){Console.WriteLine(e.Id);}//获取学院总部员工List<Employee> list2 = this.getAllEmployee();Console.WriteLine("===========学院总部员工==============");foreach (Employee e in list2){Console.WriteLine(e.Id);}}}class Program{static void Main(string[] args){//创建一个SchoolManager对象SchoolManager schoolManager = new SchoolManager();//输出学院的员工ID和学校总部的员工信息schoolManager.PrintAllEmployee(new CollegeManager());Console.ReadKey();}}
}
2)迪米特法则的实现
using System;
using System.Collections.Generic;
namespace ConsoleApplication
{//学校总部员工类class Employee{public string Id { get; set; }}//学院的员工类class CollegeEmployee{public string Id { get; set; }}//管理学院员工的管理类class CollegeManager{//返回学院的所有员工public List<CollegeEmployee> getAllEmployee(){List<CollegeEmployee> list = new List<CollegeEmployee>();//增加了10个员工到listfor (int i = 0; i < 10; i++){CollegeEmployee emp = new CollegeEmployee();emp.Id = "学院员工ID=" + i;list.Add(emp);}return list;}//输出学院员工的信息public void printEmployee(){//获取到学院员工List<CollegeEmployee> list1 = getAllEmployee();Console.WriteLine("===========学院员工==============");foreach (CollegeEmployee e in list1){Console.WriteLine(e.Id);}}}//学校管理类class SchoolManager{//返回学校总部的员工public List<Employee> getAllEmployee(){List<Employee> list = new List<Employee>();for (int i = 0; i < 5; i++){Employee emp = new Employee();emp.Id = "学校总部员工ID=" + i;list.Add(emp);}return list;}//该方法完成输出学校总部和学院员工信息(ID)public void PrintAllEmployee(CollegeManager sub){//将输出学院员工方法,封装到CollegeManagersub.printEmployee();//获取学院总部员工List<Employee> list2 = this.getAllEmployee();Console.WriteLine("===========学院总部员工==============");foreach (Employee e in list2){Console.WriteLine(e.Id);}}}class Program{static void Main(string[] args){//创建一个SchoolManager对象SchoolManager schoolManager = new SchoolManager();//输出学院的员工ID和学校总部的员工信息schoolManager.PrintAllEmployee(new CollegeManager());Console.ReadKey();}}
}