开源日报 每天推荐一个 GitHub 优质开源项目和一篇精选英文科技或编程文章原文,坚持阅读《开源日报》,保持每日学习的好习惯。
今日推荐开源项目:《战斗力不到5 genpAss》
今日推荐英文原文:《Demeter’s Law: Don’t talk to strangers!》
开源日报第562期:《战斗力不到5 genpAss》
今日推荐开源项目:《战斗力不到5 genpAss》传送门:GitHub链接
推荐理由:密码密码,顾名思义自然是不能让别人轻易知道的……这个项目是一个弱口令生成器,会根据提供的个人信息生成弱口令,简直就是完美的反面教材。如果你的密码能被别人像这样猜到,那实在是太过于尴尬了。就算是怕忘记,也有个最简单的办法:找一本英文书,选一页,把最左边的字符作为你的密码,只要你的书还没丢,就能一举两得——既不会被简单破解,也不会轻易忘记,何乐而不为呢?
今日推荐英文原文:《Demeter’s Law: Don’t talk to strangers!》作者:Carlos Caballero
原文链接:https://medium.com/better-programming/demeters-law-don-t-talk-to-strangers-87bb4af11694
推荐理由:虽然难以置信,但是老早以前的这条法规在写代码上一样适用

Demeter’s Law: Don’t talk to strangers!

Software Engineering Principles

The Law of Demeter (LoD) or principle of least knowledge is a design guideline for developing software, particularly object-oriented programs.— Wikipedia
This law was proposed by Ian Holland in 1987. Holland and colleagues were programming a system called Demeter using oriented object programming. During the development of the system, they realized that the code that fulfilled a series of rules was less coupled.

Demeter’s law is known as “don’t talk to strangers” because:
  • Each unit should have only limited knowledge about other units — only units “closely” related to the current unit.
  • Each unit should only talk to its friends — don’t talk to strangers.
  • Only talk to your immediate friends.
More formally, the Law of Demeter requires that a method m of an object O may only invoke the methods of the following kinds of objects:
  • O itself.
  • m’s parameters.
  • Any objects created/instantiated within m.
  • O’s direct component objects.
  • A global variable, accessible by O, in the scope of m.
In summary, all of the rules above can be summarized by saying you should avoid invoking methods of a member object returned by another method. In modern object-oriented languages the identifier used is dot or ->. Therefore Demeter’s law is violated when the code has more than one step between classes. For example, the following code shows an example in which Demeter’s law is violated:
开源日报第562期:《战斗力不到5 genpAss》
In this case, an object a from the A class can request a method of an object instanced of B class but the object A should not reach object B directly, because that would mean the object A has greater knowledge of object B’s internal structure (tight coupling).

The following image illustrates who are friends between class relations:
开源日报第562期:《战斗力不到5 genpAss》

Real example — Person → House → Address

Now, I am going to show a real example implemented with TypeScript. In the following UML diagram you can see as a Person is related to House and House is related to Address.
开源日报第562期:《战斗力不到5 genpAss》
The original code is from https://github.com/tavaresasilva/LoDRaV, coded using JAVA.

The following code can be run in the client/context, whereas the first code broke Demeter’s Law due to Person requiring knowledge about the inner implementation of the class House. On the other hand, the second implementation respects Demeter’s Law and the code is less coupled.
开源日报第562期:《战斗力不到5 genpAss》
The following steps show you must implement the code to respect Demeter’s Law and obtain a less coupled code. So, the first step is to create the interface which will be implemented in our concrete classes:
开源日报第562期:《战斗力不到5 genpAss》
The next step will be the implementation of the concrete classes as you can see below:
开源日报第562期:《战斗力不到5 genpAss》
开源日报第562期:《战斗力不到5 genpAss》
开源日报第562期:《战斗力不到5 genpAss》
The most important thing in the code is that no method violated Demeter’s Law (there are not more than two consecutive invocations of contained objects).

Here’s another example in which the Demeter’s Law is broken:
开源日报第562期:《战斗力不到5 genpAss》
In this case, the solution is to implement isZipCode method in class person, as you can see in the following code:
开源日报第562期:《战斗力不到5 genpAss》

Advantages

The main advantages of satisfying Demeter’s Law are:
  • Dependencies between classes and coupling are reduced.
  • Classes can be reused with ease.
  • The code is easier to test.
  • The code is more maintainable and flexible to changes.

下载开源日报APP:https://openingsource.org/2579/
加入我们:https://openingsource.org/about/join/
关注我们:https://openingsource.org/about/love/