設(shè)計模式六大原則是單一職責(zé)原則、里氏替換原則、依賴倒置原則、接口隔離原則、迪米特法則、開閉原則。它們不是要我們刻板的遵守,而是根據(jù)實際需要靈活運用。只要對它們的遵守程度在一個合理的范圍內(nèi),努為做到一個良好的設(shè)計。本文主要介紹一下.NET(C#) 迪米特法則。
?
迪米特法則(Law Of Demeter)
迪米特法則(Law of Demeter)又叫作最少知識原則(The Least Knowledge Principle),一個類對于其他類知道的越少越好,就是說一個對象應(yīng)當(dāng)對其他對象有盡可能少的了解,只和朋友通信,不和陌生人說話。迪米特法則的初衷在于降低類之間的耦合。由于每個類盡量減少對其他類的依賴,因此,很容易使得系統(tǒng)的功能模塊功能獨立,相互之間不存在(或很少有)依賴關(guān)系。
迪米特法則不希望類之間建立直接的聯(lián)系。
例如,
1)一般的反面設(shè)計實現(xiàn)
using System;
using System.Collections.Generic;
namespace ConsoleApplication
{
//學(xué)校總部員工類
class Employee
{
public string Id { get; set; }
}
//學(xué)院的員工類
class CollegeEmployee
{
public string Id { get; set; }
}
//管理學(xué)院員工的管理類
class CollegeManager
{
//返回學(xué)院的所有員工
public List<CollegeEmployee> getAllEmployee()
{
List<CollegeEmployee> list = new List<CollegeEmployee>();
//增加了10個員工到list
for (int i = 0; i < 10; i++)
{
CollegeEmployee emp = new CollegeEmployee();
emp.Id="學(xué)院員工ID=" + i;
list.Add(emp);
}
return list;
}
}
//學(xué)校管理類
class SchoolManager
{
//返回學(xué)??偛康膯T工
public List<Employee> getAllEmployee()
{
List<Employee> list = new List<Employee>();
for (int i = 0; i < 5; i++)
{
Employee emp = new Employee();
emp.Id = "學(xué)校總部員工ID=" + i;
list.Add(emp);
}
return list;
}
//該方法完成輸出學(xué)??偛亢蛯W(xué)院員工信息(ID)
public void PrintAllEmployee(CollegeManager sub)
{
//CollegeEmployee不是SchoolManager的直接朋友
//CollegeEmployee是以局部變量方式出現(xiàn)在SchoolManager違反了迪米特法則
//獲取學(xué)院員工
List<CollegeEmployee> list1 = sub.getAllEmployee();
Console.WriteLine("===========學(xué)院員工==============");
foreach (CollegeEmployee e in list1)
{
Console.WriteLine(e.Id);
}
//獲取學(xué)院總部員工
List<Employee> list2 = this.getAllEmployee();
Console.WriteLine("===========學(xué)院總部員工==============");
foreach (Employee e in list2)
{
Console.WriteLine(e.Id);
}
}
}
class Program
{
static void Main(string[] args)
{
//創(chuàng)建一個SchoolManager對象
SchoolManager schoolManager = new SchoolManager();
//輸出學(xué)院的員工ID和學(xué)??偛康膯T工信息
schoolManager.PrintAllEmployee(new CollegeManager());
Console.ReadKey();
}
}
}
?2)迪米特法則的實現(xiàn)文章來源:http://www.zghlxwxcb.cn/news/detail-670354.html
using System;
using System.Collections.Generic;
namespace ConsoleApplication
{
//學(xué)??偛繂T工類
class Employee
{
public string Id { get; set; }
}
//學(xué)院的員工類
class CollegeEmployee
{
public string Id { get; set; }
}
//管理學(xué)院員工的管理類
class CollegeManager
{
//返回學(xué)院的所有員工
public List<CollegeEmployee> getAllEmployee()
{
List<CollegeEmployee> list = new List<CollegeEmployee>();
//增加了10個員工到list
for (int i = 0; i < 10; i++)
{
CollegeEmployee emp = new CollegeEmployee();
emp.Id = "學(xué)院員工ID=" + i;
list.Add(emp);
}
return list;
}
//輸出學(xué)院員工的信息
public void printEmployee()
{
//獲取到學(xué)院員工
List<CollegeEmployee> list1 = getAllEmployee();
Console.WriteLine("===========學(xué)院員工==============");
foreach (CollegeEmployee e in list1)
{
Console.WriteLine(e.Id);
}
}
}
//學(xué)校管理類
class SchoolManager
{
//返回學(xué)??偛康膯T工
public List<Employee> getAllEmployee()
{
List<Employee> list = new List<Employee>();
for (int i = 0; i < 5; i++)
{
Employee emp = new Employee();
emp.Id = "學(xué)??偛繂T工ID=" + i;
list.Add(emp);
}
return list;
}
//該方法完成輸出學(xué)??偛亢蛯W(xué)院員工信息(ID)
public void PrintAllEmployee(CollegeManager sub)
{
//將輸出學(xué)院員工方法,封裝到CollegeManager
sub.printEmployee();
//獲取學(xué)院總部員工
List<Employee> list2 = this.getAllEmployee();
Console.WriteLine("===========學(xué)院總部員工==============");
foreach (Employee e in list2)
{
Console.WriteLine(e.Id);
}
}
}
class Program
{
static void Main(string[] args)
{
//創(chuàng)建一個SchoolManager對象
SchoolManager schoolManager = new SchoolManager();
//輸出學(xué)院的員工ID和學(xué)??偛康膯T工信息
schoolManager.PrintAllEmployee(new CollegeManager());
Console.ReadKey();
}
}
}
?文章來源地址http://www.zghlxwxcb.cn/news/detail-670354.html
到了這里,關(guān)于C#設(shè)計模式六大原則之--迪米特法則的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!