gossip

Work is hard, of course, learning to be happy. Before starting the text, make a small joke (irrelevant to the technical point of this article, can be manually filtered).

The weather is getting hotter and hotter recently, and there are more thunderstorms. These days “rookie” here has been cloudy, rain is said to come.

But one thing I’ve observed over the years is that when it rains, it tends to rain on the commute. This discovery left me confused. Am I a rain lover, or is it a recidivist?

Introduction to the

Creation:

Demeter’s principle was proposed in the fall of 1987 by Ian Holland of Northeastern University.

Basic Concepts:

1. The less a class knows about the other classes it depends on, the better. For this reason, Demeter’s law is also known as the “least known principle”. 2. Demeter has a simple definition: Talk only to your immediate friends.

The meaning of existence:

The more you know about each class, the closer the relationship, the more decoupled it becomes, and the less flexible your code becomes. The purpose of Demeter’s rule is to reduce the coupling degree between classes.

So that’s a brief introduction to Demeter’s rule, but you may not have a very clear understanding of this principle through simple text description. You don’t have to scratch your head, just keep looking down, and I’ll explain it to you in detail.

Least know principle

The meaning of “least know principle” is well understood. So how do you know the least? Take a look at the following example and think about it.

Now we need to realize the function of washing clothes by people. To do this we need two classes: a human class with a method for washing clothes. A class representing a washing machine with three methods of receiving clothes, washing and drying.

Classes that represent washing machines:

public class WashingMachine {

  // The method of receiving clothes
  public void receiveClothes(a) {
    System.out.println("Rookie washing machine, start receiving clothes!");
  }

  // The washing method
  public void wash(a) {
    System.out.println("Rookie washing machine, start washing!");
  }

  // Drying method
  public void drying(a) {
    System.out.println("Rookie washing machine, start drying clothes!"); }}Copy the code

Class of the representative:

public class Person {

  // Use the washing machine to wash clothes
  public void washClothes(WashingMachine washingMachine) {
    System.out.println("Rookie gets his clothes ready to be washed."); washingMachine.receiveClothes(); washingMachine.wash(); washingMachine.drying(); }}Copy the code

The function of washing clothes can be realized through the above two classes, but this implementation does not conform to the “least know principle”. Can you see where that is?

In fact, it is very simple, compared to the Person class, we want to implement the function of washing clothes, specific washing machine how? We don’t need to know. But the washClothes method in the Person class above calls all three methods of the Washington Machine class. And these three methods are the washing machine needs to do, with us people have nothing to do. This results in the Person class knowing too much about the Washington Machine class.

The solution to this problem is also very simple, its root is in the class itself, from the root constraints themselves, as far as possible not to expose some of their own methods and attributes. Simply put, each class has its own secret. This allows other classes to know less about themselves.

Based on the above analysis, make the following adjustments to the above code.

Optimized washing machine category:

public class WashingMachine {

  // Automatic laundry
  public void automatic(a) {
    this.receiveClothes();
    this.wash();
    this.drying();
  }

  // The method of receiving clothes
  private void receiveClothes(a) {
    System.out.println("Rookie washing machine, start receiving clothes!");
  }

  // The washing method
  private void wash(a) {
    System.out.println("Rookie washing machine, start washing!");
  }

  // Drying method
  private void drying(a) {
    System.out.println("Rookie washing machine, start drying clothes!"); }}Copy the code

Class of representative after optimization:

public class Person {

  // Use the washing machine to wash clothes
  public void washClothes(WashingMachine washingMachine) {
    System.out.println("Rookie gets his clothes ready to be washed."); washingMachine.automatic(); }}Copy the code

That’s the least know rule. Pretty simple, isn’t it? It tells us to privatize some of our own methods and attributes when creating a class.

Talk to a direct friend

Classes that appear in member variables, method parameters, and method return values are direct friends of the class, while classes that appear in local variables are not direct friends. Talking can simply be understood as using other classes. The idea is to try not to use unfamiliar classes in the form of local variables in this class.

To give you an idea, take a look at the following example code (the function of this code is for the project manager to count employees).

Programmer class:


public class Programmer {

  / / name
  private String name;

  public String getName(a) {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  // Get the programmer
  public List<Programmer> getProgrammers(a) {
    List<Programmer> list = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
      Programmer p = new Programmer();
      p.setName(i + "Number farming");
      list.add(p);
    }
    returnlist; }}Copy the code

Project Manager:

public class ProjectManager {

  / / name
  private String name;

  // constructor
  ProjectManager(String name) {
    this.name = name;
  }

  // Count the number of employees
  public void countEmployees(a){
    System.out.println(name + "Start counting programmers!");
    Programmer programmer = new Programmer();
    List<Programmer> list = programmer.getProgrammers();
    for(Programmer p : list) { System.out.println(p.getName()); }}}Copy the code

The test class:

public class Demo {

  public static void main(String[] args) {
    ProjectManager projectManager = new ProjectManager("Rookie"); projectManager.countEmployees(); }}Copy the code

Look at ProjectManager’s countEmployees method. The Programmer class used by this method is created from local variables, so the Programmer class is an alien class. We used strange classes, which don’t fit the “only talk to direct friends” rule.

The above code can be optimized according to Demeter’s rule as follows:

// Modify the countEmployees method of the ProjectManager class
public void countEmployees(Programmer programmer){
    System.out.println(name + "Start counting programmers!");
    List<Programmer> list = programmer.getProgrammers();
    for(Programmer p : list) { System.out.println(p.getName()); }}Copy the code

We pass in the Programmer class as a method entry, making the Programmer class a “direct friend” of the ProjectManager. By doing so, our program conforms to the “only talk to direct friends” rule.

Pay attention to

Demeter’s rule is to reduce the degree of coupling between classes (objects), is to reduce unnecessary dependencies, does not require the complete absence of dependencies.

We need to use it moderately, not overdo it. A completely independent class has no value and no meaning.

This is the end of today’s sharing, if you feel that the article written by “rookie” is still good, remember to like and pay attention to yo! Your support is what keeps me going.

Article where to write problems also hope that you can point out, I will be modestly taught. If there is any knowledge you want to know, please tell me in the comment section. If there is anything I know, I will explain it in detail for you.