デメタルの法則は、オブジェクトを返すメソッドを書くことができなかったように見えるので非常に混乱しています。

StackOverflow https://stackoverflow.com/questions/5050083

質問

私はデッドエンドに来たように感じます。 Demeter の法則に従う場合は、それを理解していたら、返すメソッドを作成することはできません。オブジェクトとその後クライアントコードはそれを呼び出します。私は常にオブジェクトを返す工場パターンについて考えています。はい、オブジェクトを返すマッパークラスがあります。そしてコレクションはどうですか?

役に立ちましたか?

解決

You've misunderstood the Law of Demeter and are applying it beyond the point of usefulness:

More formally, 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

Factories in particular are used to create an object, and the type of object they create is part of their public interface. Thus, calling methods of an object created by a factory is allowed by the above.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top