更詳細的說法是“Law of Demeter for Functions/Methods” (LoD-F),簡單化Object之間的互動,讓多個Object不要互相相依。
定義:
The Law of Demeter for functions 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
範例:
1 | /** |