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
3
votes
3 answers

Does Passive View breaks the Law of Demeter?

I'm trying to understand how to use Passive View correctly. It seems to me that every examples I look at on Passive View breaks the Law of Demeter : //In the presenter code myview.mytextfield.text = "whatever"; So what's a better implementation of…
subb
  • 1,578
  • 1
  • 15
  • 27
3
votes
3 answers

Law Of Demeter on Factory Pattern and Dependency Injection

I have a question regarding dependency injection. say i want to create a class call it, WebGetTask WebGetTask would need a dependency to HttpService bad code 1 Code: private HttpService httpService; ... List list = new…
Titi Wangsa bin Damhore
  • 7,101
  • 4
  • 31
  • 36
3
votes
3 answers

does iterator pattern violates law of demeter? ( least knowldge principle)

class MyCollection { Items menuItems; ... public Iterator createIterator() { return new Iterator(menuItems); } } class Client { public someMethod() { Iterator iterator = collection.createIterator(); …
Jerry
  • 399
  • 1
  • 2
  • 17
3
votes
4 answers

How to restructure this code hierarchy (relating to the Law of Demeter)

I've got a game engine where I'm splitting off the physics simulation from the game object functionality. So I've got a pure virtual class for a physical body class Body from which I'll be deriving various implementations of a physics simulation.…
Jesse Beder
  • 33,081
  • 21
  • 109
  • 146
3
votes
1 answer

c++ Dependency Injection + Law of Demeter + logger/assert

I've seen two great videos (this and this) about dependency injection, law of demeter and global states (Singletons are considered as global). I think I got the basic idea but I already have some singleton classes in my library. However if I want a…
csisy
  • 471
  • 3
  • 15
3
votes
1 answer

Aggregate Root Data Duplication and the Law of Demeter

The Law of Demeter is a rule that says that an object should only be aware of "closely related" other objects (my interpretation). See https://en.wikipedia.org/wiki/Law_of_Demeter. The following examples do not follow the LoD: // This class has to…
magnus
  • 4,031
  • 7
  • 26
  • 48
3
votes
1 answer

Law of Demeter violation search tool?

Does anybody know of a tool that I could use with a C# application to find possible Law of Demeter violations? I know that it would give a lot of false positives, but I think it could still be useful. Especially during the early design process.
Jerod Houghtelling
  • 4,783
  • 1
  • 22
  • 30
3
votes
4 answers

Law of Demeter: Static property access

I have a line of code looking like this: String someString = "something"; if (Foo.SOME_CONSTANT_STRING.equals(someString)) which results in a violation: "Potential violation of Law of Demeter (static property access)" What is the suggested approach…
Alix
  • 2,630
  • 30
  • 72
3
votes
1 answer

IntelliJ Refactor to use LoD

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 class Bar { protected Foo foo; public Bar() { this.foo =…
3
votes
4 answers

Encapsulation Principle

There's some object-oriented engineering principle that states something along the lines of "a class should only know about the contracts of the classes that it takes as arguments, or any internal ones it uses." The counter-example, in C++,…
Clayton Hughes
  • 1,015
  • 2
  • 9
  • 22
3
votes
0 answers

How to use merge in Rails 4 join operation correctly?

I do have a following code structure: class Asset < ActiveRecord::Base belongs_to :state scope :order_by_year, -> { joins(:state).merge(State.order_by_year) } ... class State < ActiveRecord::Base belongs_to :financial_statement has_many…
hade
  • 1,715
  • 1
  • 16
  • 24
2
votes
2 answers

Integrity of Law of Demeter preserved by using helper function (removed two dots)?

public House { WeatherStation station; public float getTemp() { //Law of Demeter has been violated here return station.getThermometer().getTemperature(); } } public House { WeatherStation station; public float…
Vadim
  • 1,223
  • 2
  • 17
  • 26
2
votes
1 answer

Law of Demeter/single responsibility when events are involved

I am trying to reconcile the Law of Demeter for programming environments where events are involved - I tagged this javascript and obj-c (Cocoa's NSNotificationCenter) because both allow for events. In such an environment, you can arbitrarily…
ambertch
  • 7,581
  • 4
  • 28
  • 40
2
votes
3 answers

Ruby / Rails: create a class method that operates on instances of its children?

In my app, Photo has_and_belong_to_many :land_uses I have this helper method in the Photo model: def land_use_list land_uses.map(&:name).join(', ') end This strikes me as a code smell (demeter), but I haven't been able to figure out how to move…
Andrew
  • 42,517
  • 51
  • 181
  • 281
2
votes
3 answers

Is it appropriate to repeat data in models to satisfy using law of demeter in collections?

This is a contrived example, say I want to list the population of a country that a person has a friend in, here are two setups below. Would it be best to repeat data in the models? I've been told that the Law of Demeter is important to follow, the…
thejonster
  • 156
  • 2
  • 10