Law of Demeter

In this article, we will understand the Law of Demeter (LoD), the principle of least knowledge. It is a design guideline to develop software, particularly in the object-oriented (OO) world.

Ian Holland introduced this law in 1987 when he was working on the Demeter project. Demeter is the Greek goddess of Agriculture. The LoD got its name from its initial use in the Demeter project.

In simple definition this law says each unit should talk to its friends; don’t talk to strangers! The LoD focuses on loose coupling. Loose coupling simply means that changes in one component least affect the existence or performance of other components in a software system. Loosely coupled software systems are easier to maintain and test.

Let’s look into a more detailed explanation of the law:

The Law of Demeter says that a method f of class C should only call:

  1. Other methods in the class(C) directly.
  2. Methods of an object passed as an argument to f.
  3. The methods of an object created by f.
  4. Methods of an object held in an instance variable of C.

Let’s understand with an example. Consider a class “Customer”(C) with two methods:

  • generateInvoice (f)
  • getFullName
class Customer{

    public Customer(IService service)
    {
      this.service = service;
    }

    public void generateInvoice(Param param)
    {
	   // 1. Other methods in the class
	   getFullName();
	   
	   // 2. Methods of an object passed as an argument to function
	   param.doSomething();
	   
	   // 3. Methods of object created by f
	   TempObject temp = new TempObject();
	   temp.process();
	   
	   // 4. Methods of object held in instance variable of the class
	   service.provideService()
    }

    private String getFullName()
    {
      ...
    }
};

Let’s examine generateInvoice function (f) line by line.

 // 1. Other methods in the class
getFullName();

The generateInvoice can call other methods in the class, for example, getFullName method in the Customer class.

// 2. Methods of an object passed as an argument to function
param.doSomething();

We can see that param object is passed as an argument to the function. So generateInvoice can call the methods of param object (param.doSomething()).

	   // 3. Methods of object created by f
	   TempObject temp = new TempObject();
	   temp.process();

Here new object(temp) is created inside the generateInvoice and it can call the methods of the temp object.

	   // 4. Methods of object held in instance variable of the class
	   service.provideService()

Here generateInvoice method can call the methods of an object held as an instance variable(service).

In all four cases, the method is talking to the methods inside the class, passed objects, or instance variables. In other words, it is talking only to friends.

Advantages

  1. The software that results from adhering to the Law of Demeter has a tendency to be more maintainable and adaptable.
  2. LoD implementation is best suited for multi-layered architecture. Code within each layer can only make calls to code within the layer and code within the next layer down.

Disadvantage

  1. The use of LoD may result in having to write many wrapper methods to propagate call to components which increase time and space overhead.