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?