Law of Demeter (LoD) of Java Design Patterns

    Law of Demeter, Demeter is the goddess of agriculture, grain and harvest in ancient Greek mythology , one of the twelve main gods of Olympus . It takes its name from Project Demeter, which was named in honor of Demeter, the "distribution-mother" and the Greek goddess of agriculture, to denote a bottom-up programming philosophy.

    The specific meaning of the Dimit law can be understood from the following sentences:

  • Each unit should have only limited knowledge about other units: only units "closely" related to the current unit.
  • Each unit should only talk to its friends; don't talk to strangers.
  • Only talk to your immediate friends.

which is:

  • Each unit should have very limited knowledge of other units: only those units that are closely related to the current unit.
  • Each unit only talks to friends, not strangers.
  • Only speak to your immediate friends.

   Let's take a look at an example:

 //总公司员工
    class Employee{
        private String id;
        public void setId(String id){
            this.id = id;
        }
        public String getId(){
            return id;
        }
    }

    //分公司员工
    class SubEmployee{
        private String id;
        public void setId(String id){
            this.id = id;
        }
        public String getId(){
            return id;
        }
    }

    class SubCompanyManager{
        public List getAllEmployee(){
            List list = new ArrayList();
            for(int i=0; i<100; i++){
                SubEmployee emp = new SubEmployee();
                //为分公司人员按顺序分配一个ID
                emp.setId("分公司"+i);
                list.add(emp);
            }
            return list;
        }
    }

    class CompanyManager{

        public List getAllEmployee(){
            List list = new ArrayList();
            for(int i=0; i<30; i++){
                Employee emp = new Employee();
                //为总公司人员按顺序分配一个ID
                emp.setId("总公司"+i);
                list.add(emp);
            }
            return list;
        }

        public void printAllEmployee(SubCompanyManager sub){
            List list1 = sub.getAllEmployee();
            for(SubEmployee e:list1){
                System.out.println(e.getId());
            }

            List list2 = this.getAllEmployee();
            for(Employee e:list2){
                System.out.println(e.getId());
            }
        }
    }

    public class Client{
        public static void main(String[] args){
            CompanyManager e = new CompanyManager();
            e.printAllEmployee(new SubCompanyManager());
        }
    }

    Modified as follows:

class SubCompanyManager{  
    public List<SubEmployee> getAllEmployee(){  
        List<SubEmployee> list = new ArrayList<SubEmployee>();  
        for(int i=0; i<100; i++){  
            SubEmployee emp = new SubEmployee();  
            //为分公司人员按顺序分配一个ID  
            emp.setId("分公司"+i);  
            list.add(emp);  
        }  
        return list;  
    }  
    public void printEmployee(){  
        List<SubEmployee> list = this.getAllEmployee();  
        for(SubEmployee e:list){  
            System.out.println(e.getId());  
        }  
    }  
}  

class CompanyManager{  
    public List<Employee> getAllEmployee(){  
        List<Employee> list = new ArrayList<Employee>();  
        for(int i=0; i<30; i++){  
            Employee emp = new Employee();  
            //为总公司人员按顺序分配一个ID  
            emp.setId("总公司"+i);  
            list.add(emp);  
        }  
        return list;  
    }  

    public void printAllEmployee(SubCompanyManager sub){  
        sub.printEmployee();  
        List<Employee> list2 = this.getAllEmployee();  
        for(Employee e:list2){  
            System.out.println(e.getId());  
        }  
    }  
}

    Following the Law of Demeter can reduce the coupling between classes, and each class reduces unnecessary dependencies. However, if it is overused, there will be a large number of intermediary classes, so the pros and cons should be weighed and not blindly.

 

Quote:

http://wiki.jikexueyuan.com/project/java-design-pattern-principle/principle-5.html

https://en.wikipedia.org/wiki/Law_of_Demeter

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324409515&siteId=291194637