Re: Opinions on the Law Of Demeter
From: Andrew McDonagh (news_at_andrewcdonagh.f2s.com)
Date: 10/27/04
- Next message: H. S. Lahman: "Re: Global variables"
- Previous message: ShadowMan: "Re: Global variables"
- In reply to: H. S. Lahman: "Re: Opinions on the Law Of Demeter"
- Next in thread: H. S. Lahman: "Re: Opinions on the Law Of Demeter"
- Reply: H. S. Lahman: "Re: Opinions on the Law Of Demeter"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 27 Oct 2004 19:27:34 +0100
H. S. Lahman wrote:
> Responding to Daniel T....
>
>>> The problem is that the law itself is stated ambiguously. What is a
>>> "close" or "immediate" friend? FWIW, I would argue that LoD is
>>> honored in the basic way one approaches OOA/D.
>>
>>
>>
>> Actually, the law (as I understand it) is very concrete. The version
>> on that web page is what is ambiguous. Here it is as I understand it:
>>
>> a method of an object should only call methods belonging to: itself,
>> any parameters that were passed in to it, or any objects that were
>> created from inside it.
>
>
> I've never seen that definition. FWIW, I think it is essentially
> unusable as stated. By that definition the only way to have
> collaborations (i.e., invoke methods in other objects not part of its
> own implementation) is by passing the other object as a reference in
> method arguments. That form of relationship instantiation is very poor
> OO practice. (The exception, of course, are knowledge setters that
> instantiate a relationship as a referential attribute.)
>
>>
>> That last bit is important. Sure "a = new A()" is a creation, but so
>> is "a = b.getA()" /if/ 'getA' returns an independent object. For example:
>>
>> // Java
>> class B {
>> private A a;
>> public A getA() { return a.clone(); }
>> }
>
>
> <Java question>
> I don't know Java, but your example triggers an interesting question
> about it for me. Does "private A a;" cause a default constructor to be
> invoked as it would in C++? In C++ I could implement
No, all that line of code is doing is declaring a reference of type A
called 'a', but its not currently initialized to anything other than null.
The following code would have the same result:
private A a = null;
There are only references in Java, no pointers in the C/C++ sense.
Therefore theres no syntax necessary for differentiating.
>
> 1 1 1
> [Client] ------------- [B] <*>------------ [A]
>
> class B {
> private:
> A myA; // embedded object; default constructor
> public:
> A& navigateToA () {return &myA;}
> }
>
class B {
public A navigateToA() {return myA;}
private A myA = new A(); // creates an embedded object in java.
}
> or
>
> 1 1 1 1
> [Client] ------------ [B] --------------- [A]
>
> class B {
> private:
> A* myA; // referential attribute; initialized explicitly
> public:
> B (A* a) {myA = a; ...};
> void instantiateA (A* a) {myA = a;};
> A* navigateToA () {return myA;};
> }
class B {
public B(A a) {myA = a;}
public void instantiateA (A a) {myA = a;}
private A myA; // will be instantiated by either CTor or other method
}
>
> Without separate pointer syntax, how does one distinguish the two
> implementations in Java?
There are only referneces in Java, no pointers in the C/C++ sense.
Therefore theres no syntax necessary for differeniating.
Class B either instantiates myA itself for the first or provides means
of allowing others to instantiate an object of type A and set it via the
CTor of some method.
Andrew
- Next message: H. S. Lahman: "Re: Global variables"
- Previous message: ShadowMan: "Re: Global variables"
- In reply to: H. S. Lahman: "Re: Opinions on the Law Of Demeter"
- Next in thread: H. S. Lahman: "Re: Opinions on the Law Of Demeter"
- Reply: H. S. Lahman: "Re: Opinions on the Law Of Demeter"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|