Advanced Object Oriented Design Laboratory Blazej.Pietrzak@cs.put.poznan.pl
Message Chains
Law of Demeter Principle Object- Oriented style rule for builing systems (Dem et er Research Group 1987, published 1988) General Form: Each unit should only use a limited set of other units: only units closely related to the current unit Don t talk to strangers
Law of Demeter Principle cont. friends
Object Form of Law of Demeter Principle unit = method Closely related: 3. A parameter of the method 4. The enclosing object (this). 5. A static object. 6. An immediate part object (computed or stored): a. An object that the enclosing method returns b. At tributes of the enclosing object c. An element of a collection which is an attribute of the enclosing object 7. An object created within the method.
Object Form of Law of Demeter Principle unit = method Closely related: 3. A parameter of the method 4. The enclosing object (this). 5. A static object. public void dosth(object obj) { System.out.println(obj.toString()); this.dosth( LoD );
Object Form of Law of Demeter Principle 1. An immediate part object (computed or stored): a. An object that the enclosing method returns public class SthClass {... public Object getobj() { return new Object(); public void dosth() { System.out.println(getObj().toString());
Object Form of Law of Demeter Principle 1. An immediate part object (computed or stored): a. At tributes of the enclosing object b. An element of a collection which is an attribute of the enclosing object public class SthClass { public List collection;... public void dosth() { Object elem = collection.get(0); System.out.println(elem.toString());
Object Form of Law of Demeter Principle 1. An object created within the method. public void dosth() { Object obj = new Object(); System.out.println(obj.toString());
Law of Demeter Principle - Motivation Programmers can only keep a limited set of items in short- term memory and it is easier to keep them in memory if they are closely related Hiding the structure of navigation causes less changes to client. Reusability of m ethods (low coupling)
Law of Demeter Principle The Strong Law of Demeter The instance variables make up a given class. Inherited instance variable types may not be passed messages. The Weak Law of Demeter The instance variables and any instance variables inherited from other classes make up a given class
What is Message Chains? Message Chains is a violation of the Law of Demeter Principle
example public class Person { private Department department; public void setdepartment(department department){ this.department = department; public Department getdepartment() { return department;
example cont. public class Department { public Department(Person manager) { this.manager = manager; public Person getmanager() { return manager;
example cont....... Person manager = john.getdepartment().getmanager(); Violation of the Law of Demeter
example public class Person { private Department department; public void setdepartment(department department){ this.department = department; public Department getdepartment() { return department; public Person getmanager() { return department.getmanager(); Extract to method Move method Hide delegate Correct