Definition: An object should have minimal knowledge of other objects
The core idea of Dimitri law is decoupling between classes, weak coupling, only weak coupling, the reusability of class can be improved.
The analogy is similar to: prison inmates should not contact with outside people, of course, may have relatives. The prison here is the class, the inmates inside are the information inside the class, and the prison guards are the Dimitri of the law.
The Dimitri Law advocates:
(1) in the division of classes, it is necessary to create weakly coupled classes;
(2) In the class structure design, each class should minimize the members ' access rights;
(3) in the design of class, whenever possible, a class should be designed as invariant class;
(4) in reference to other classes, an object's reference to other objects should be minimized;
(5) Minimizing the access rights of classes;
(6) Careful use of serialization functions;
(7) Do not expose class members, but should provide the corresponding accessors (properties).
To cite an example: there is a group company, subordinate units have branches and immediate departments, now require the printing of all subordinate units of the employee ID. Let's take a look at the design that violates the Dimitri law.
Head Office staff class employee{private String ID;
public void SetId (String id) {this.id = ID;
Public String GetId () {return id;
}//Company employee Class subemployee{private String ID;
public void SetId (String id) {this.id = ID;
Public String GetId () {return id; Class subcompanymanager{public list<subemployee> Getallemployee () {list<subemployee> List = n
EW arraylist<subemployee> ();
for (int i=0; i<100; i++) {subemployee emp = new Subemployee ();
Assign an ID emp.setid ("branch" +i) to the branch staff in order;
List.add (EMP);
} return list; Class companymanager{public list<employee> Getallemployee () {list<employee> List = new Arra
Ylist<employee> ();
for (int i=0; i<30; i++) {Employee EMP = new Employee ();
Assign an ID emp.setid ("head Office" +i) in order for head office personnel;
List.add (EMP);
} return list; } public void PrintallemplOyee (Subcompanymanager sub) {list<subemployee> List1 = Sub.getallemployee ();
for (Subemployee e:list1) {System.out.println (E.getid ());
} list<employee> 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 ());
}
}
Now the main problem with this design is in Companymanager, according to the Dimitri Law, only with direct friends, The Subemployee class is not a direct friend of the Companymanager class (the coupling that occurs with local variables is not a direct friend), and logically the head office is only coupled to his branch office, and there is no contact with the staff of the branch, so the design obviously adds unnecessary coupling. In accordance with the Dimitri rule, you should avoid the coupling of such indirect relationships in a class. The modified code is as follows:
Class subcompanymanager{public list<subemployee> Getallemployee () {list<subemployee> List = new Arra
Ylist<subemployee> ();
for (int i=0; i<100; i++) {subemployee emp = new Subemployee ();
Assign an ID emp.setid ("branch" +i) to the branch staff in order;
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 A
Rraylist<employee> ();
for (int i=0; i<30; i++) {Employee EMP = new Employee ();
Assign an ID emp.setid ("head Office" +i) in order for head office personnel;
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 ());
}
}
}
modified to increase the number of print staff ID for the branch office, the direct use of the company to print, thereby avoiding coupling with the staff of the branch. The original purpose of the
Dimitri law is to reduce coupling between classes, because each class reduces unnecessary dependencies, so it does reduce the coupling relationship. But everything has the degree, although can avoid with not direct class communication, but to communicate, will inevitably through an "intermediary" to contact, for example, in this case, the head office through the branch of this "intermediary" to contact with the staff of the branch. Excessive use of the Dimitri principle, will produce a large number of such intermediary and transfer classes, resulting in a greater complexity of the system. Therefore, the use of Dimitri rules to be weighed repeatedly, not only to achieve a clear structure, but also high cohesion low coupling.