So the law of demeter essentially aims to reduce coupling between modules. I am working on writing out some examples to ensure that I understand the concept conceptually. The code that I have written is below, and would just like clarification. From my understanding the set of preferred classes include: the classes of instance variables of class car, argument classes of method foo or the class car itself. Because of the 3rd line where truck is being acted upon via a method of the car class itself, it is my understanding that this upholds the law of demeter. Can anyone give me clarification?
public void foo( vehicle car ) {
vehicle truck = car;
truck.do(truck);
}
The above code does not
Yes, it does violate the law of Demeter. It reaches through truck to get to car.do(), using truck (an alias for parameter car) as a parameter. Car is being passed itself as a parameter)