Re: Opinions on the Law Of Demeter

From: Andrew McDonagh (news_at_andrewcdonagh.f2s.com)
Date: 10/27/04


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



Relevant Pages

  • Re: The Java no pointer big fat lie!
    ... > reference types, it wouldn't be a good comparison. ... and what you can't - and there is a big difference in Java to C and C++. ... You cannot change it in the language itself, ... Thus I would distinguish pointers from ...
    (comp.lang.java.programmer)
  • Re: How long to learn C++ if fluent in Java?
    ... I know this "Java has no pointers" mantra. ... The point is, however, that Java reference behave much more like C++ ... unlike with democracy, for some purposes there is indeed a better tool to ...
    (comp.lang.cpp)
  • Re: why java does not support pointers
    ... "An object is a class instance or an array. ... The reference values are pointers to these ... Java supports pointers. ...
    (comp.lang.java.programmer)
  • Re: Trouble with Java concepts
    ... >>references when you see a variable which names an object in Java think of it as ... I would try to explain to an incoming C/C++ programmer that passing objects ... is functionally identical to passing pointers in C. ... use of pointers is implicit (you cannot have an Object, only a reference) ...
    (comp.lang.java.programmer)
  • Re: How java passes object references?
    ... Which everybody knows, it doesn't in Java. ... The reason I think this is a useful clarification is that when you got to the part about how passing by reference might work, it seems you went off track at least partly because you didn't understand the nature of the above. ... is a pointer pointing at the memory block. ... Assignments to local variables, or even to class members, do not allocate memory. ...
    (comp.lang.java.programmer)