開源日報 每天推薦一個 GitHub 優質開源項目和一篇精選英文科技或編程文章原文,堅持閱讀《開源日報》,保持每日學習的好習慣。
今日推薦開源項目:《戰鬥力不到5 genpAss》
今日推薦英文原文:《Demeter』s Law: Don』t talk to strangers!》

今日推薦開源項目:《戰鬥力不到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:

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:

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.

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.

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:

The next step will be the implementation of the concrete classes as you can see below:



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:

In this case, the solution is to implement isZipCode method in class person, as you can see in the following code:

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/