I'm studying for an oral exam, and I wonder if I have understood Law of Demeter correctly. In essence, I have understood that the Law of Demeter aims to loosen coupling by making classes less dependent on one another, and by not giving away explicitly how the classes get certain information. This is concluded in the quote "Only talk to your immediate friends". I have come up with this simplified example:
If we have a class Board and we have game pieces on our playing field and for example want to find out which pieces have moved an intuitive way to find this out would be to write something like:
Board.getGamePiece(p).getMovement();
But this violates the Law of Demeter. We thus delegate that task to the getMovement method by simply writing:
Board.getMovement(p);
Have I understood it correctly and are there any errors within my explanation? I am a bit unsure as to how to use the term "delegate" here, is it used correctly in my example?
question from:
https://stackoverflow.com/questions/65599765/is-this-a-good-example-of-law-of-demeter 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…