title: How to obey Law of Demeter?
body: <p>Any time I see articles about Law of Demeter the author never seems to give a solid example of how to obey this law. They all explain what it is and show an example of breaking the law but that is easy.</p>
  
  <p>There is probably lots of ways to obey this law (good design and planning being one) but in very simple terms would this be a way to obey it?</p>
  
  <p>Let's say I have a class with these properties:</p>
  
  <pre><code>public class Band {
  
      private Singer singer;
      private Drummer drummer;
      private Guitarist guitarist;
  }
  </code></pre>
  
  <p>I am somewhere in the program and I have an instance of this <code>Band</code> class and I want the guitarists name, what I usually see is something like:</p>
  
  <pre><code>guitaristName = band.getGuitarist().getName();
  </code></pre>
  
  <p>That one does not seem too bad as it is not going too deep in the chain but is Law of Demeter saying that maybe it should be done this way:</p>
  
  <pre><code>guitaristName = band.getGuitaristName();
  </code></pre>
  
  <p>and my <code>Band</code> class has a method:</p>
  
  <pre><code>public String getGuitaristName() {
      return guitarist.getName();
  }
  </code></pre>
  
  <p>Is this how you are supposed to obey the law?</p>
  
  <p>Thanks.</p>