This is the fifth day of my participation in the August More text Challenge. For details, see:August is more challenging

preface

The Demeter principle is always followed by the phrase, “At least know the principle, the less one class knows about the other, the better.” Today we’re going to look for Demeter in code examples, in aggregate examples when does Demeter apply? Demeter’s strengths and weaknesses? Is code that complies with Demeter’s principles good code? With these questions in mind, let’s begin our discussion.

Di milt

The Law of Demeter says that if two classes do not have to communicate directly with each other, then the two classes should not interact directly. If one class needs to call a method of the other class, the call can be forwarded through a third party.

In the case of any object, we should only call methods within that object’s methods that fall within the following scope:

  • The object itself
  • An object passed in as an argument to a method
  • Any object created or instantiated by this method
  • Any component of the object

In conclusion, Demeter’s principle is to keep method calls within bounds and not to call methods that are outside the bounds.

If we run into problems along the way, let’s go back to the definition of Demeter’s law.

The code examples

I’m going to show you four examples, and you can think about which of these examples fit the Demeter principle? What don’t? What are your criteria and why? (Feel free to discuss and spark in the comments section)

demo1

The Client side

public class client {
    public static void main(String[] args) {
        Station station = newStation(); Thermometer thermometer = station.getThermometer(); System.out.println(thermometer.getTemperature()); }}Copy the code

Station Station

public class Station {
    public Thermometer getThermometer(a) {
        return newThermometer(); }}12345Copy the code
  • 1
  • 2
  • 3
  • 4
  • 5

Thermometer Thermometer

public class Thermometer {
    public double getTemperature(a) {
        return 10; }}Copy the code

demo2

The Client side

public class Client {
    public static void main(String[] args) {
        Station station = new Station();
        doubletemperature = station.getTemperature(); System.out.println(temperature); }}Copy the code

Station Station

public class Station {
    public double getTemperature(a) {
        Thermometer thermometer = new Thermometer();
        returnthermometer.getTemperature(); }}Copy the code

Thermometer Thermometer

public class Thermometer {
    public double getTemperature(a) {
        return 10; }}Copy the code

demo3

The Client side

public class Client {
    public static void main(String[] args) {
        Station station = newStation(); System.out.println(station.getTermomoter().getTemperature()); }}Copy the code

Station Station

public class Station {
    public Thermometer getThermometer(a) {
        return newThermometer(); }}Copy the code

Thermometer Thermometer

public class Thermometer {
    public double getTemperature(a) {
        return 10; }}Copy the code

demo4

The Client side

public class Client {
    public static void main(String[] args) {
        System.out.println(new Client().getTemp());
    }
    public double getTemp(a) {
        Station station = new Station();
        Thermometer thermometer = station.getThermometer();
        return getTempHelper(thermometer);
    }
    public double getTempHelper(Thermometer thermometer) {
        returnthermometer.getTemperature(); }}Copy the code

Station Station

public class Station {
    public Thermometer getThermometer(a){
        return newThermometer(); }}Copy the code

Thermometer Thermometer

public class Thermometer {
    public double getTemperature(a) {
        return 10; }}Copy the code

Reveal the demo veil

After analyzing the 4 demos, which ones conform to The Demeter principle? Listen to me slowly analysis, since it is analysis, it must be reasoned, below I will combine the class diagram of each instance and code comparison analysis.

demo1

In the class diagram generated by the code, we can see that the Client is directly related to both the Station and Thermometer classes. Does one class directly relate to both classes violate the Demeter principle? After a while, we’ll look at the code to see if the Client needs to be directly associated with these two classes. We can see that the Client’s ultimate goal is to get the temperature. Is it really necessary to have a purpose that is directly related to two classes? It’s perfectly possible to use the Station to get the temperature, without having a direct contact with the Thermometer. By definition, if a class needs to call a method of another class, it can forward the call through a third party. The third person here is Station. Therefore, it is easy to conclude that Demo1 does not conform to The Demeter principle.

demo2

After looking at the class diagram, it is found that the Client only has a direct connection with the Station, which conforms to the definition mentioned above. Forwarding the call (between the Client and the Station) via the Station is a typical example that conforms to the Demeter principle.

demo3

We find that the relationship between classes of demo3 and Demo2 is exactly the same. Does it follow, then, that Demo3 also conforms to The Demeter principle? For a moment, we can see that demo3 is programmed in a linked way. Instead of displaying any Thermometer object declared in the Client, it implicitly calls the Thermometer object returned by the station call. How do you define the Demeter rule in this case? It is found in the class diagram that there is no direct connection between the Client and the Thermometer, but according to the code, the Thermometer method can be called through the object returned by the method called by the station object. So the Client is still directly related to the Station and Thermometer, but it just doesn’t show the claims. Conclusion: Demo3 violates the Demeter principle.

demo4

After checking the class chart, I find that Client has a direct relationship with Station and Thermometer classes. Is it possible to conclude that… The truth is often not so simple (to glasses), see this version of the example, is not want to scold on a sentence, this is not to take off pants **, obviously a method can be completed, not to split into two methods to call. Conclusion: Demo4 is based on the Demeter principle. When analyzing the problem, let’s go back to the definition of the principle. If a class needs to call a method of another class, it can forward the call through a third party. In Demo2, the third party is Station, and the call is forwarded through the Station class. Let’s analyze the definition, what is this third party? In Demo2, the third party is a class, while in Demo4, the third party is a method. Calls are forwarded through the method, and only one member method of the class is called in a method. As we said above, there are four Demeter criteria within any object’s method, and demo4 is all of them. Decoupling is achieved by forwarding calls in this way. But let’s ask ourselves, is this really necessary?


The advantages and disadvantages of The Demeter principle

The advantage of the Demeter principle, as we all know, is to reduce the coupling between classes. After all, the weaker the coupling between classes, the more reuse is possible. A weakly coupled class can be modified without affecting the related classes.

The advantages must have disadvantages, that is not always in line with the Demeter principle? Adopting the Demeter principle leads to more “wrapper” classes being manufactured to handle communication with other components, which can lead to increased complexity and development time, and reduced runtime performance.

Therefore, it is not necessary to conform to The Demeter principle all the time, and too much reliance on the Demeter principle will bring a lot of “side effects”.

conclusion

How to determine if the code is in compliance with the Demeter principle? From the experience analyzed above, it is concluded that:

  • You can’t just look at the class diagram. You need to analyze the code in combination with the class diagram
  • In combination with the definition of principle, the final conclusion must not violate the concept