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
70
votes
6 answers

Coupling, Cohesion and the Law of Demeter

The Law of Demeter indicates that you should only speak to objects that you know about directly. That is, do not perform method chaining to talk to other objects. When you do so, you are establishing improper linkages with the intermediary…
Bradley Mazurek
  • 941
  • 9
  • 9
38
votes
11 answers

How to solve the violations of the Law of Demeter?

A colleague and I designed a system for our customer, and in our opinion we created a nice clean design. But I'm having problems with some coupling we've introduced. I could try to create an example design which includes the same problems as our…
Davy Landman
  • 15,109
  • 6
  • 49
  • 73
17
votes
5 answers

Law of Demeter in API Design for C++

In his book API Design for C++, Martin Reddy elaborates on the Law of Demeter. In particular, he states that: you should never call a function on an object that you obtained via another function call. He supports his statement with a chaining…
Korchkidu
  • 4,908
  • 8
  • 49
  • 69
16
votes
1 answer

Simple Custom Refactoring in IntelliJ

This question is a follow-up for this. Say I have some class Foo. class Foo { protected String x = "x"; public String getX() { return x; } } I have a program that uses Foo and violates LoD (Law of Demeter). class Bar { …
16
votes
7 answers

Java: Accessing resources and the Law Of Demeter

Overview In my (Android) Java game, I have a class called resources. As the name suggests, this class holds the resources for the game. All of my OpenGL objects (Sprites) are created here It's looks something like the following (obviously, this is…
Zippy
  • 3,826
  • 5
  • 43
  • 96
16
votes
3 answers

Does deferred / promise promote breaking the Law of Demeter?

I was in the shower and thought about something. The deferred / promise pattern is to decrease callback hell, by allowing the developer to chain call functions, as mentioned here: Parse.User.logIn("user", "pass").then(function(user) { return…
corgrath
  • 11,673
  • 15
  • 68
  • 99
15
votes
3 answers

Law of Demeter with data model objects

I came back to work from vacation yesterday, and in our daily standup, my teammates mentioned they were refactoring all of the model objects in our java code to remove all getters and setters and make the model fields all public objects instead,…
alexD
  • 2,368
  • 2
  • 32
  • 43
14
votes
1 answer

When should I use delegate, and when should I use has_one :through?

Rails has two nice ways to avoid Law of Demeter violations in models. The first is this: class Restaurant < ActiveRecord::Base belongs_to :franchise delegate :owner, to: :franchise end The second is this: class Restaurant <…
henrebotha
  • 1,244
  • 2
  • 14
  • 36
14
votes
3 answers

Applying the LAW of DEMETER with a facade pattern

In essential skills for the agile developer, in the needs vs capabilities interface, chap, 12, I'm trying to understand the main solution proposed to the challenge of applying the LAW OF DEMETER that the author mention by the end of this chapter.…
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
13
votes
3 answers

What is Law of Demeter?

Let's start with Wikipedia: More formally, the Law of Demeter for functions requires that a method m of an object O may only invoke the methods of the following kinds of objects: O itself m's parameters Any objects created/instantiated within…
Anonymous
  • 363
  • 3
  • 13
13
votes
3 answers

How do the Law of Demeter and composition with collections work together?

I have read nearly all of the questions tagged Law-of-Demeter. My specific question is not answered in any of these other questions, though it is very similar. Mainly my question is when you have an object with layers of composition, but the need to…
Scott Shipp
  • 2,241
  • 1
  • 13
  • 20
11
votes
2 answers

A factory method may violate the Law Of Demeter?

quoting from here: https://en.wikipedia.org/wiki/Law_of_Demeter More formally, the Law of Demeter for functions requires that a method m of an object O may only invoke the methods of the following kinds of objects:[2] O itself m's…
John Smith
  • 6,129
  • 12
  • 68
  • 123
10
votes
2 answers

Can multiple operations with Streaming break The Law of Demeter?

I went little fancy and wrote Selenium page-object with Java 8 streaming as mentioned in below code and got a review comment that my code is breaking Law of Demeter, since I am doing lot of operations in a single line. I was suggested to break the…
10
votes
5 answers

Law of Demeter - Data objects

I'm trying to follow the Law Of Demeter ( see http://en.wikipedia.org/wiki/Law_of_Demeter , http://misko.hevery.com/code-reviewers-guide/flaw-digging-into-collaborators/ ) as I can see the benefits, however I've become a little stuck when it comes…
Tom B
  • 2,735
  • 2
  • 24
  • 30
9
votes
1 answer

Law of Demeter is very confusing because looks like I couldn't ever write methods that return objects

It feels like I've come to a dead end. If I understood it right then if I follow the Law of Demeter I can never make a method that returns an object and then client code makes calls to it. I'm just thinking about the Factory Pattern which always…
PPPHP
  • 747
  • 10
  • 20
1
2 3
9 10