Questions tagged [law-of-demeter]

The Law of Demeter (LoD) or Principle of Least Knowledge is a design guideline for developing software, particularly object-oriented programs. In its general form, the LoD is a specific case of loose coupling.

The Law of Demeter (LoD) or Principle of Least Knowledge is a design guideline for developing software, particularly object-oriented programs. In its general form, the LoD is a specific case of loose coupling:

  • Each unit should have only limited knowledge about other units: only units "closely" related to the current unit;
  • Each unit should only talk to its friends; don't talk to strangers;
  • Only talk to your immediate friends.

The fundamental notion is that a given object should assume as little as possible about the structure or properties of anything else (including its subcomponents).

137 questions
5
votes
2 answers

Law of Demeter confusion

I'm hoping someone can help explain the law of demeter to me. If I have a class which I'm assuming is an aggregate root and within that have a collection of child classes is it illegal to update the properties of those child classes by accessing…
mageets
  • 91
  • 7
5
votes
1 answer

Granularization of models?

I'm developing a CMS largely based on Zend Framework components. Some of the database tables for this CMS are as followed: site | id | name | ------------- locale | languageCode | regionCode | ----------------------------- site_locale // link…
Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
5
votes
3 answers

Law of Demeter and Class Constructors

The Law of Demeter does not prevent passing objects into class constructors. However, it does forbid getting that same object back later and calling a method on it to get a scalar value out. Instead, a proxy method is supposed to be created that…
Joe
  • 115
  • 7
5
votes
2 answers

How to design class dependency trying to avoid Law of Demeter

Ok,I´ve searched and couldn´t find a suitable solution for my problem, I am redesigning a part of our point of sale system. Let´s suppose we have the following classes: TWorkShift = class Date: TDateTime; fTotalSold: Currency; fSales:…
Luis Carrasco
  • 467
  • 3
  • 10
4
votes
5 answers

Law of demeter or return the whole vector

Which one is better: public: const vector & GetPointsVector(); private: vector PointsVector; Or: public: int GetCurrentPoint(); void MoveToFirstPoint(); void MoveToNextPoint(); bool IsAtLastPoint(); size_t…
Igor
  • 26,650
  • 27
  • 89
  • 114
4
votes
2 answers

Law of Demeter - how far do you go?

I want to follow the Law of Demeter. As I am going through my code searching for the "two dots", I find myself asking if it's really worth setting up the delegation responsibilities in this type of context: FROM class Activity def…
keruilin
  • 16,782
  • 34
  • 108
  • 175
4
votes
3 answers

This also violates Demeter's law? Or it would be an overkill to warp it?

a very simple point: class Point { private $x, $y; public function __constructor($x, $y) { $this->x = $x; $this->y = $y; } public function getX() { return $this->x; } public function getY() …
John Smith
  • 6,129
  • 12
  • 68
  • 123
4
votes
3 answers

Law of demeter - using only one dot, could I improve this logic?

I have the following method: private boolean reserveSeat(int selectedRow, int selectedSeat) { if (show.getRows().get(selectedRow).getSeats().get(selectedSeat).getReservationStatus()) { return false; } else { …
Urban Gemz
  • 75
  • 4
4
votes
4 answers

Should I care that passing in a class representation of an XML settings file violates the law of demeter?

I'm using a tool to automatically generate a class representation of a hierarchically organized XML file. The XML file is a settings file my app need to be able to access (read-only). If I pass in the top-level node (e.g., AppSettings) to a class…
devuxer
  • 41,681
  • 47
  • 180
  • 292
4
votes
2 answers

Do getters violate the Law of Demeter?

Imagine there was a GameState type which uses a GameContext (via a process method): abstract class GameState { public abstract void process(GameContext context); } GameContext would contain things such as the Player, Shops, ect.. things that…
Vince
  • 14,470
  • 7
  • 39
  • 84
4
votes
1 answer

Does embedding in golang violate law of demeter?

This is what the Effective GO had to say about Embedding in golang When we embed a type, the methods of that type become methods of the outer type, but when they are invoked the receiver of the method is the inner type, not the outer one I had a…
Anony-mouse
  • 2,041
  • 2
  • 11
  • 23
4
votes
0 answers

PMD Warning "Potential violation of Law of Demeter : object not created locally", even on calling methods on local objects.

What I understood about law of demeter is : Your method can call other methods in its class directly Your method can call methods on its own fields directly (but not on the fields' fields) When your method takes parameters, your method can…
shashi009
  • 720
  • 6
  • 23
4
votes
5 answers

How does this code break the Law of Demeter?

The following code breaks the Law of Demeter: public class Student extends Person { private Grades grades; public Student() { } /** Must never return null; throw an appropriately named exception, instead. */ private synchronized Grades…
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
4
votes
2 answers

Exercise 26 of The Pragmatic Programmer

There is a code snippet presented in The Pragmatic Programmer on page 143 as: public class Colada { private Blender myBlender; private Vector myStuff; public Colada() { myBlender = new Blender(); myStuff = new Vector(); …
Ande Turner
  • 7,096
  • 19
  • 80
  • 107
4
votes
1 answer

How to lazy load while obeying Law of Demeter?

I would like to obey Law of Demeter. But I would also like to lazy load some objects passed to constructors. How should I implement that? Pass a wrapper class? Pass a function pointer?
fxam
  • 3,874
  • 1
  • 22
  • 32
1 2
3
9 10