0

I'm trying to get better understand of Law of Demeter in a real world (aka my application), but I have some confuses about reasons and benefits which I get when resigning of a chain of responsibilities.

I have an example, where I am considering usage. There are classes with relations

Enquiry -> assignedTo -> Room Rooms -> assignedTo -> Building Building -> assignedTo -> Company

Now I need to access some company data, ex. company name, and I have access to Enquiry. So my current flow is: enquiry.getManager().getRoom().getBuilding().getCompany().getName() - pretty long, isn't it?

I think if I follow LoD it should be changed to enquiry.getHostingCompanyName() but it seems to me that I need to create room.getHostingCompanyName() and building.getHostingCompanyName() upfront, and as a result, it will be very fragile and will require much more changes than the previous approach when it comes to refactoring.

Can you provide any pieces of advice on that? Or maybe my assumptions are totally wrong, and it should be done in a different way?

acabala
  • 149
  • 1
  • 14

2 Answers2

3

You are not using objects but data structures only. You write something like:

new Human().getStomach().getContent().insert(new Cake());

instead of

new Human().eat(new Cake());

Real objects should encapsulate / hide internal structure, and expose domain specific API. Also for me it's little weird that Room contains Company.

In my opinion Room should be unaware of companies, instead you should obtain room references from Company object.

It seems to my that your object structure is driven by the database, but it is not good.

All above have sense when it's applied to rich domain system and not for some trivial CRUDs. In CRUD systems data structures works better.

Michał Mielec
  • 1,582
  • 1
  • 14
  • 38
0

The real world issue of the Law of Demeter (or the Principle of Least Knowledge) goes something like this:

Oh drat, just realized buildings aren't always assigned to just one company. Going to have to change the interface of this class. That's a breaking change. Well better round up this classes friends and change them... Why the heck is this causing 101 errors? How many friends does one class need?

The Law of Demeter is really about encapsulation. It's a kind of encapsulation you don't hear much about. Normally encapsulation is about what is in an object. Here it's about knowledge of the object itself.

If your object has a few friends that deal with the rest of the world so it doesn't have to then the object is actually encapsulated even though it's public. This stops being true if objects from the outside world reach into the object through those friends. Now your object could be known by any object. That's a lot to have to fix. Objects hide behind their friends. The don't want to be popular. They want to do a little bit of work for a few close friends. They want to be encapsulated.

That's why if you have to ask a friend of a friend to do something you're breaking this encapsulation. You should tell your friends to do things for you and not worry about who their friends are. You have enough friends.

The point then is if you find your self needing to reach through classes to get what you need it means some work has been left undone that would allow you to not even know the class you're reaching for. Do the work and stop being so friendly with everything.

If you need more I've actually written about this before.

Community
  • 1
  • 1
candied_orange
  • 7,036
  • 2
  • 28
  • 62