Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object Oriented Programming: Separation of Data and Behavior

Tags:

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.
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).

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     }  } 

Now, separate out the behavior and data into separate classes.

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     } } 

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

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;     } } 

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.
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.

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 ?

like image 261
devanalyst Avatar asked Jul 09 '12 06:07

devanalyst


1 Answers

Actually, Martin Fowler says that in the domain model, data and behavior should be combined. Take a look at AnemicDomainModel.

like image 125
user1168577 Avatar answered Sep 29 '22 18:09

user1168577