1024programmer Java Understanding the Dimit Principle in Java Design Pattern Programming

Understanding the Dimit Principle in Java Design Pattern Programming

Definition: An object should keep minimal knowledge of other objects.
Origin of the problem: The closer the relationship between classes, the greater the degree of coupling. When one class changes, the greater the impact on the other class.
Solution: Try to reduce the coupling between classes.
Since we have been exposed to programming, we have known the general principles of software programming: low coupling and high cohesion. Whether it is process-oriented programming or object-oriented programming, only by keeping the coupling between modules as low as possible can the code reuse rate be improved. The advantages of low coupling are self-evident, but how can we achieve low coupling through programming? That’s exactly what Demeter’s Law is meant to accomplish.
The Dimett is also called at least the principle of knowing the principle. It was first proposed by the Ian Holland of Northeastern University in the United States in 1987. In layman’s terms, the less a class knows about the classes it depends on, the better. In other words, for the dependent class, no matter how complex the logic is, the logic should be encapsulated inside the class as much as possible, and no information will be leaked to the outside except the public methods provided. Demeter’s Law has a simpler definition: only communicate with direct friends. First, let’s explain what a direct friend is: each object will have a coupling relationship with other objects. As long as there is a coupling relationship between two objects, we say that the two objects are friends. There are many ways of coupling, such as dependency, association, combination, aggregation, etc. Among them, we call classes that appear in member variables, method parameters, and method return values ​​direct friends, while classes that appear in local variables are not direct friends. In other words, it is best not for unfamiliar classes to appear inside the class as local variables.

The following is an example of violating Demeter’s principle:

 public class Teacher {
  public void teach(Classes classes){


What’s the problem? Coupling is too high.
1. The Score class may be canceled in the Student class.
2. The show method of Score class may also be deleted.
The Student class and Score class are all unfamiliar to you, and you may not know when they change.
We can modify it to:

 public class Teacher {
  public void teach(Classes classes){
 public class Classes {
  public void showScore(Student student){
 public class Student {
  Score score;
  public void showScore(){
   score = new Score(80);


1. The advantage of Dimit’s law is to reduce the coupling between classes.
2. The disadvantage is that it will generate more small methods, making the system more messy, and the communication efficiency will be reduced.
3. Application in design patterns: Facade Pattern and Mediator Pattern.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/703491

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us


Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top