Deameter의 법은 물체를 반환하는 방법을 작성할 수 없었기 때문에 매우 혼란 스럽습니다.
-
15-11-2019 - |
문제
그것은 내가 막 다른 끝에 오는 것처럼 느낍니다.내가 Deameter의 법칙을 따르는 경우 의 법칙을 따르는 경우 나는 결코 반환하는 방법을 만들 수 없다.객체 및 클라이언트 코드는 호출을 만듭니다.항상 객체를 반환하는 공장 패턴에 대해 생각하고 있습니다.예, 객체를 반환하는 매퍼 클래스가 있습니다.컬렉션은 어떻습니까?
해결책
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.
제휴하지 않습니다 StackOverflow