Six Principles of C# Design Patterns--Dimit's Law

The six principles of the design pattern are the single responsibility principle, the Liskov substitution principle, the dependency inversion principle, the interface isolation principle, Dimit's law, and the opening and closing principle. They are not for us to obey rigidly, but to use them flexibly according to actual needs. As long as the degree of compliance with them is within a reasonable range, try to achieve a good design. This article mainly introduces the .NET (C#) Dimiter's Law.

 

Law Of Demeter

Law of Demeter (Law of Demeter) is also called the least knowledge principle (The Least Knowledge Principle), the less a class knows about other classes, the better, that is to say, an object should know as little as possible about other objects, as long as Correspondence with friends, not strangers. The original intention of Dimit's law is to reduce the coupling between classes. Since each class minimizes its dependence on other classes, it is easy to make the functional modules of the system independent, and there is no (or very little) dependency relationship between them.

Demeter's law does not want to establish a direct connection between classes.

For example,

1) General negative design implementation

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个员工到list
            for (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) Realization of Demeter's Law

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个员工到list
            for (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)
        {
            //将输出学院员工方法,封装到CollegeManager
            sub.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();
        }
    }
}

 

Guess you like

Origin blog.csdn.net/lwf3115841/article/details/132467955