title: Object Oriented Programming: Separation of Data and Behavior
body: <p>Recently we had a discussion regarding Data and Behavior separation in classes. The concept of separation of Data and Behaviour is implemented by placing the Domain Model and its behavior into seperate classes.<br/> 
  However I am not convinced of the supposed benefits of this approach. Even though it might have been coined by a "great" (I think it is Martin Fowler, though I am not sure). I present a simple example here. Suppose I have a Person class containing data for a Person and its methods (behavior).</p>
  
  <pre class="lang-cs prettyprint-override"><code>class Person
  {
      string Name;
      DateTime BirthDate;
  
      //constructor
      Person(string Name, DateTime BirthDate)
      {
          this.Name = Name;
          this.BirthDate = BirthDate;
      }
  
      int GetAge()
      {
          return Today - BirthDate; //for illustration only
      }
  
  }
  </code></pre>
  
  <p>Now, separate out the behavior and data into separate classes.</p>
  
  <pre class="lang-cs prettyprint-override"><code>class Person
  {
      string Name;
      DateTime BirthDate;
  
      //constructor
      Person(string Name, DateTime BirthDate)
      {
          this.Name = Name;
          this.BirthDate = BirthDate;
      }
  }
  
  class PersonService
  {
      Person personObject;
  
      //constructor
      PersonService(string Name, DateTime BirthDate)
      {
          this.personObject = new Person(Name, BirthDate);
      }
  
      //overloaded constructor
      PersonService(Person personObject)
      {
          this.personObject = personObject;
      }
  
      int GetAge()
      {
          return personObject.Today - personObject.BirthDate; //for illustration only
      }
  }
  </code></pre>
  
  <p>This is supposed to be beneficial and improve flexibility and provide loose coupling. I do not see how. According to me this introduces extra coding and performance penalty, that each time we have to initialize two class objects. And I see more problems in extending this code. Consider what happens when we introduce inheritance in above case. We have to inherit both the classes</p>
  
  <pre class="lang-cs prettyprint-override"><code>class Employee: Person
  {
      Double Salary;
  
      Employee(string Name, DateTime BirthDate, Double Salary): base(Name, BirthDate)
      {
          this.Salary = Salary;       
      }
  
  }
  
  class EmployeeService: PersonService
  {
      Employee employeeObject;
  
      //constructor
      EmployeeService(string Name, DateTime BirthDate, Double Salary)
      {
          this.employeeObject = new Employee(Name, BirthDate, Salary);
      }
  
      //overloaded constructor
      EmployeeService(Employee employeeObject)
      {
          this.employeeObject = employeeObject;
      }
  }
  </code></pre>
  
  <p>Note that even if we segregate out the behavior in a seperate class, we still need object of the Data class for the Behaviour class methods to work on. So in the end our Behavior class contains both the data and the behavior albeit we have the data in form of a model object.<br/> 
  You might say that you can add some Interfaces to the mix , so we could have IPersonService and an IEmployeeService. But I think introducing interfaces for each and every class and inherting from interfaces does not seem OK.<br/><br/> 
  So then can you tell me what have I achieved by seperating out the data and behavior in above case that I could not have achieved by having them in the same class ?</p>