Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we sometimes separate behaviour from classes in Java [closed]

Tags:

java

oop

class

dto

Its a pretty basic question but I am new to Java designing to please excuse me. :)

I want to know in which scenarios we need to separate the class behavior from the class itself.

for e.g.

If I have an class Employee, I will have some data in it like - name, age etc. Also this class will have some behavior like doWork() etc. Now in what scenario we can have data and the behavior inside once class (Employee) only and in which scenario we need to have 2 different classes for Employee data (EmployeeDTO) and behavior (EmployeeService)

Very subjective question but am looking for some inputs on a design of a small application where I am taking data from a text file. Should I put the data and behavior in different classes or same? What will be your reason to justify this decision?

PS: Any links to information on this will also be very useful :)

Thankyou

like image 814
Vishal Avatar asked Aug 28 '11 13:08

Vishal


2 Answers

Good object-oriented design advocates that each class obey the Single Responsibility Principle. Which I can't summarize any more eloquently than the wikipedia entry:

Martin defines a responsibility as a reason to change, and concludes that a class or module should have one, and only one, reason to change. As an example, consider a module that compiles and prints a report. Such a module can be changed for two reasons. First, the content of the report can change. Second, the format of the report can change. These two things change for very different causes; one substantive, and one cosmetic. The single responsibility principle says that these two aspects of the problem are really two separate responsibilities, and should therefore be in separate classes or modules. It would be a bad design to couple two things that change for different reasons at different times.

If you think about it, you could jam all of your Java code into one class file, but you don't. Why? Because you want to be able to change, maintain, adapt and test it. This principle says that instead of dividing your code up arbitrarily into different modules, you should take the tact that things should be broken up by their logical responsibilities. This generally means more, small modules which we know to be easier to change, maintain, adapt and test.

I personally would recommend that you factor your code out into smaller discrete classes and combine them later if this proves to be unreasonable -- this will become obvious to you. Its much easier to combine loosely-coupled code in the future than it is to factor out tightly-coupled code.

like image 94
jonathan.cone Avatar answered Oct 24 '22 12:10

jonathan.cone


Do the simplest thing possible. You can always make your code more generalized later and there's a good chance you won't even have to do it.

Apply YAGNI principle every time you need to make a decision. Extreme Programming wiki is also a nice reading.

Put everything into one class right now. When you see your Employee is getting too fat then you can do some refactoring - for example, move method to another class. In statically typed languages like Java it is super easy because compiler helps a lot and IDE support is great.

Reading from file, for example, looks like an obvious candidate to extract to a separate loader class. On the other hand if you have a very common format as input such as XML or JSON you could just create static method List<Employee> Employee.loadFromFile(string fileName) and implement reading logic in a couple of lines of code. It's good enough right now: simple, short and works fine.

May The Real Ultimate Programming Power be with you!

like image 38
Konstantin Spirin Avatar answered Oct 24 '22 12:10

Konstantin Spirin