title: JavaWorld on OO: Getters/Setters vs Builder
body: <h2>Background:</h2>
  
  <p>I found <a href="http://www.javaworld.com/javaworld/jw-01-2004/jw-0102-toolbox.html?page=4" rel="noreferrer">this article</a> on JavaWorld, where Allen Holub explains an alternative to Getters/Setters that maintains the principle that the implementation of an object should be hidden (his example code can also be found below).</p>
  
  <p>It is explained that the classes <code>Name</code>/<code>EmployeeId</code>/<code>Money</code> should have a constructor taking a single string - the reasoning is that if you type it as an <code>int</code>, and later need to change it to a <code>long</code>, you will have to modify all the uses of the class, and with this pattern you don't have to.</p>
  
  <h2>Question 1:</h2>
  
  <p>I was wondering: doesn't this simply move the problem to the parsing of the <code>String</code> parameters being tossed about? For example, if all the code using the <code>EmployeeId</code> (received from the <code>Exporter</code>) parses the <code>String</code> into an <code>int</code>, and suddenly you start exporting <code>long</code> values, you need to modify exactly as many uses... and if you start out parsing it as a <code>long</code> it might well have to change to a <code>double</code> (even though that makes no sense for id's)... and if you can't be sure what to parse the <code>String</code> into, you <em>can't implement anything</em>.</p>
  
  <h2>Question 2:</h2>
  
  <p>Besides this question, I have another: I realise that the article is over seven years old, so could anyone point me to some recent overviews concerning OO-design, and specifically to ideas concerning the getter/setter and implementation hiding debate?</p>
  
  <h2>Listing 1. Employee: The Builder Context</h2>
  
  <hr>
  
  <pre><code>  public class Employee
    {   private Name        name;
        private EmployeeId  id;
        private Money       salary;
  
        public interface Exporter
        {   void addName    ( String name   );
            void addID      ( String id     );
            void addSalary  ( String salary );
        }
  
        public interface Importer
        {   String provideName();
            String provideID();
            String provideSalary();
            void   open();
            void   close();
        }
  
        public Employee( Importer builder )
        {   builder.open();
            this.name   = new Name      ( builder.provideName()     );
            this.id     = new EmployeeId( builder.provideID()       );
            this.salary = new Money     ( builder.provideSalary(),
                                      new Locale("en", "US") );
            builder.close();
        }
  
        public void export( Exporter builder )
        {   builder.addName  ( name.toString()   );
            builder.addID    ( id.toString()     );
            builder.addSalary( salary.toString() );
        }
  
        //...
    }
  </code></pre>