Dahua Design Pattern Nine: The Law of Demeter

One monk carries water to eat, two monks carry water to eat, and three monks have no water to eat.


Even if there are two people, there should be management.


I'm a side dish, the computer is broken, if I don't repair it, the software will stop workinglaughing out loud



The Law of Demeter (LoD) is also called the principle of least knowledge.


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


The premise that the Law of Demeter first emphasizes is that in the structural design of the class, each class should minimize the access rights of members, that is, a class wraps its own private state, and does not need to let other classes know the fields or behavior should not be made public.



Object-oriented design principles and the three characteristics of object-oriented are not contradictory.


The fundamental idea of ​​the Law of Demeter is to emphasize the loose coupling between classes.



The weaker the coupling between classes, the more conducive to reuse. A class in weak coupling is modified without affecting related classes.


That is, the hiding of information promotes the reuse of software.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325420094&siteId=291194637