SDSU CS 696: Advanced OO
Spring Semester, 1997
Doc 5, Law of Demeter

To Lecture Notes Index
San Diego State University -- This page last updated Feb 10, 1997
----------

Contents of Doc 5, Law of Demeter

Law of Demeter
Law of Demeter slide # 2
...Benefits slide # 6
...Law of Demeter and God Classes slide # 7
...Law of Demeter and Container Classes slide # 10



Doc 5, Law of Demeter Slide # 1
References

Lieberherr, Holland, Riel, Object-Oriented Programming: An Objective Sense of Style, OOPSLA 1988 Proceedings

Lieberherr, Adaptive Object-Oriented Software: The Demeter Method with Propagation Patterns, PWS Publishing Company, 1996


Doc 5, Law of Demeter Slide # 2

Law of Demeter

Weak Form

Inside of a method M of a class C, data can be accessed in and messages can be sent to only the following objects:

Law of Demeter
Strong Form


class Course {
     Instructor boring = new Instructor();
     int pay = 5;

     public Instructor getInstructor() { return boring; }

     public Instructor getNewInstructor() {return new Instructor(); }

     public int getPay() {return pay; }

}

class C {
     Course test = new Course();

     public void badM() {
          test.getInstructor().fired();
     }

     public void goodM() {
          test.getNewInstructor().hired();
     }

     public int goodOrBadM?() {
          return getpay() + 10;
     }
}

Doc 5, Law of Demeter Slide # 4
Correcting badM


class Course {
     Instructor boring = new Instructor();
     int pay = 5;

     public Instructor fireInstructor() { boring.fired(); }

     public Instructor getNewInstructor() { return new Instructor();}

     public int getPay() { return pay ; }
}

class C {
     Course test = new Course();

     public void reformedBadM() { 
          test.fireInstructor(); 
     }

     public void goodM() {
          test.getNewInstructor().hired();
     }

     public int goodOrBadM() {
          return  getpay() + 10;
     }
}

Doc 5, Law of Demeter Slide # 5
Making goodM Useful

class Course {
     Instructor boring = new Instructor();
     int pay = 5;

     public Instructor fireInstructor() { boring.fired(); }

     public Instructor hireNewInstructor() { boring = new Instructor();}

     public int getPay() { return pay ; }
}

class C {
     Course test = new Course();

     public void reformedBadM() { test.fireInstructor(); }

     public void goodMImproved() {     test.hireNewInstructor();}

     public int goodOrBadM() {
          return  getpay() + 10;
     }
}


Doc 5, Law of Demeter Slide # 6
Law of Demeter

Benefits



Doc 5, Law of Demeter Slide # 7

Law of Demeter and God Classes


Grader program with God class

Assume that Student and Scores are basicly structs with lots of get/set methods and Course does all the work

class Student
     {
     Scores     exams = new Scores();

     public Scores getExamScores()     
          { return exams; }

     public Scores setExamScores( Scores newScores)     
          { exams = newScores;}

     // stuff missing
     }

class Course
     {
     Student[]  enrolled = new Student[ 40 ];

     public void computeAverage( int studentIndex )
          {
          Scores toCompute = 
               enrolled[ studentIndex].getExamScores();

          float sum = 0;
          for ( int k = 0; k < toCompute.length(); k ++ )
               sum += toCompute.gradeAt( k );
          }
     }
Doc 5, Law of Demeter Slide # 8
Law of Demeter and God Classes

Following the law leads us to putting operations in Student and Scores classes

Compare coupling in two examples

class StudentImproved
     {
     Scores     exams = new Scores();

     public int computeAverage()     
          {
          // compute average here;
          }

     public Scores setExamScores( Scores newScores)     
          {
          exams = newScores;
          }
     }

class Course
     {
     StudentImproved[]  enrolled = new StudentImproved[ 40 ];

     public void computeAverage( int studentIndex )
          {
          int average = enrolled[ studentIndex].computeAverage();
          }
     }

Doc 5, Law of Demeter Slide # 9
Law of Demeter and God Classes
Another solution?

class Student
     {
     Scores     exams = new Scores();

     public Scores getExamScores()     { return exams; }

     public Scores setExamScores( Scores newScores)     
          { exams = newScores;}

     // stuff missing
     }

class Course
     {
     Student[]  enrolled = new Student[ 40 ];

     public void computeAverage( int studentIndex )
          {
          computeStudentAverage(
               Student[ studentIndex].getExamScores());
          }

     public void computeStudentAverage( Score exams )
          {
          float sum = 0;
          for ( int k = 0; k < exams.length(); k ++ )
               sum += exams.gradeAt( k );
          }
     }

Doc 5, Law of Demeter Slide # 10

Law of Demeter and Container Classes

class Test
     {
     public void ok( Scores exam, Scores homework )
          {
          exam.hereIsAMethodCall();
          homework.hereIsAMethodCall();
          }

     public void seemsOK( Scores[2] gradeEvents )
          {
          gradeEvents[0].hereIsAMethodCall();
          gradeEvents[1].hereIsAMethodCall();
          }

     public void seemsNotOK( Vector gradeEvents )
          {
          gradeEvents.at(0).hereIsAMethodCall();
          gradeEvents.at(1).hereIsAMethodCall();
          }
     }





----------