Tuesday, March 25, 2014

Law of Demeter - Refactoring to Hide Delegate

The Law of Demeter is a design guideline for developing software, particularly object-oriented programs. In its general form, the law is a specific case of loose coupling. The fundamental notion of the law is that a given object should assume as little as possible about the structure or properties of anything else (including its subcomponents), in accordance with the principle of "information hiding".
For E.g. the code given below shows that the client calls the method on the dependent object of the client rather than on the object itself. This increases the coupling between classes.
var manager = john.GetDepartment().GetManager();
The Hide Delegate refactoring principle can be applied to this design to solve the issue.

public Manager GetManager()

    return Department.GetManager();


No comments: