Six Design principles

Single responsibility principle

Richter’s substitution principle

Dependency inversion principle

Interface Isolation Principle

Demeter’s rule

Open and closed principle

define

One object should know as little as possible about other objects.

The sample

Tom and David are friends, and David and Eva are friends. If Tom wants to be friends with Eva, he must know her through David.

The sample a

public class Tom { public void play(David david){ david.play(); } public void play(Eva eva) { eva.play(); }} public class David {public void play(){system.out.println (" make friends with David "); }} public class Eva {public void play(){system.out.println (" make friends with Eva "); } } public class Test { public static void main(String[] args) { Tom tom = new Tom(); David david = new David(); Eva eva = new Eva(); tom.play(david); tom.play(eva); }}Copy the code

This example implementation is friends with Eva, but Tom is directly associated with Eva.

Example 2

public class Tom { public void play(David david){ david.play(); Eva eva = david.leadEva(); eva.play(); }} public class David {public void play(){system.out.println (" make friends with David "); } public Eva leadEva() {system.out.println (" leadEva "); Eva eva = new Eva(); return eva; }} public class Eva {public void play(){system.out.println (" make friends with Eva "); } } public class Test { public static void main(String[] args) { Tom tom = new Tom(); David david = new David(); tom.play(david); }}Copy the code

In this scheme, David and Eva become friends, but Tom contains the reference to Eva, which does not conform to Demeter’s rule.

Example 3

public class Tom { private David david; public David getFriend() { return david; } public void setFriend(David david) { this.david = david; } public void play(David david){ david.play(); }} public class David {public void play(){system.out.println (" make friends with David "); playWithStranger(); } public void playWithStranger() { Eva eva = new Eva(); eva.play(); }} public class Eva {public void play(){system.out.println (" make friends with Eva "); } } public class Test { public static void main(String[] args) { Tom tom = new Tom(); David david = new David(); tom.setFriend(david); tom.play(david); }}Copy the code

This program has no connection with Eva, and we become friends.

Example 4

Define an abstraction for Eva in combination with the dependency inversion principle.

public class Tom { private David david; private Eva eva; public Tom(David david, Eva eva) { this.david = david; this.eva = eva; } public David getDavid() { return david; } public void setDavid(David david) { this.david = david; } public Eva getEva() { return eva; } public void setEva(Eva eva) { this.eva = eva; } public void play(){ david.play(); eva.play(); }} public class David {public void play(){system.out.println (" make friends with David "); } } public abstract class AbstractEva { public abstract void play(); } public class Eva extends AbstractEva {public void play(){system.out.println (" make friends with Eva "); } } public class Test { public static void main(String[] args) { Tom tom = new Tom(new David(), new Eva()); tom.play(); }}Copy the code

What Tom communicates directly with is the abstract superclass of Eva, which is not directly related to the concrete implementation of Eva, so it conforms to Demeter’s law.

conclusion

The core concept of Demeter’s law is decoupling and weak coupling between classes. Only after the weak coupling, the reuse rate of classes can be improved. However, this will also reduce the communication efficiency between different modules of the system, and make it difficult to coordinate between different modules of the system.