装潢公司网站建设,led论坛网站建设,软件商城源码,wordpress同步腾讯微博设计模式六大原则是单一职责原则、里氏替换原则、依赖倒置原则、接口隔离原则、迪米特法则、开闭原则。它们不是要我们刻板的遵守#xff0c;而是根据实际需要灵活运用。只要对它们的遵守程度在一个合理的范围内#xff0c;努为做到一个良好的设计。本文主要介绍一下.NET(C#)…设计模式六大原则是单一职责原则、里氏替换原则、依赖倒置原则、接口隔离原则、迪米特法则、开闭原则。它们不是要我们刻板的遵守而是根据实际需要灵活运用。只要对它们的遵守程度在一个合理的范围内努为做到一个良好的设计。本文主要介绍一下.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 ListCollegeEmployee getAllEmployee(){ListCollegeEmployee list new ListCollegeEmployee();//增加了10个员工到listfor (int i 0; i 10; i){CollegeEmployee emp new CollegeEmployee();emp.Id学院员工ID i;list.Add(emp);}return list;}}//学校管理类class SchoolManager{//返回学校总部的员工public ListEmployee getAllEmployee(){ListEmployee list new ListEmployee();for (int i 0; i 5; i){Employee emp new Employee();emp.Id 学校总部员工ID i;list.Add(emp);}return list;}//该方法完成输出学校总部和学院员工信息IDpublic void PrintAllEmployee(CollegeManager sub){//CollegeEmployee不是SchoolManager的直接朋友//CollegeEmployee是以局部变量方式出现在SchoolManager违反了迪米特法则//获取学院员工ListCollegeEmployee list1 sub.getAllEmployee();Console.WriteLine(学院员工);foreach (CollegeEmployee e in list1){Console.WriteLine(e.Id);}//获取学院总部员工ListEmployee 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 ListCollegeEmployee getAllEmployee(){ListCollegeEmployee list new ListCollegeEmployee();//增加了10个员工到listfor (int i 0; i 10; i){CollegeEmployee emp new CollegeEmployee();emp.Id 学院员工ID i;list.Add(emp);}return list;}//输出学院员工的信息public void printEmployee(){//获取到学院员工ListCollegeEmployee list1 getAllEmployee();Console.WriteLine(学院员工);foreach (CollegeEmployee e in list1){Console.WriteLine(e.Id);}}}//学校管理类class SchoolManager{//返回学校总部的员工public ListEmployee getAllEmployee(){ListEmployee list new ListEmployee();for (int i 0; i 5; i){Employee emp new Employee();emp.Id 学校总部员工ID i;list.Add(emp);}return list;}//该方法完成输出学校总部和学院员工信息IDpublic void PrintAllEmployee(CollegeManager sub){//将输出学院员工方法封装到CollegeManagersub.printEmployee();//获取学院总部员工ListEmployee 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();}}
}