Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is POJO anti Object Orientation (OO)?

In most definitions, POJO (Plain Old Java Object) is object with properties along with just getters/setters. Some people name these DTO. So essentially they are what i like to call them 'Data classes'. Whatever the name is i know there are these kind of objects where it just have getters/setters. And these are commonly used within developing multi-layered MVC web applications - where these objects would be passed through the different layers.

Maybe my question is a bit towards opinionated - but is this pure OOP or not? I'm determined to find out the answer to this i.e. what's right and what's wrong?

...Because, i have been brought up learning that OOP is all about modelling real-life scenarios/situations into objects through interaction. With objects themselves encapsulate data and behaviour which acts on this data. - an object with just getters/setters doesn't fit into this definition in my opinion.

Also, data classes are bad (code smell) - that's what i've been taught in university.

like image 438
SoftwareDeveloper Avatar asked Sep 04 '15 15:09

SoftwareDeveloper


1 Answers

POJO and XXService is bad idea! Any action(method) belong to an Object.

OK, just look a sample, it's people's simple action:

Class People {
    string name;
}

Class PeopleService{
   public void pee(People people){
      System.out.print(people.name + " relax...");
   }   
}

aha, now we need extend it..., we know some difference between man and woman

class Man extends People{}

class Woman extends People{}

if only one service, you should write:

Class PeopleService {
   public void pee(People people){
      if(people instanceof Man)//if ... and if ...
          System.out.println("stand up");
      if(people instanceof Woman)
          System.out.println("sit down");
      System.out.print(people.name + " relax...");
   }  
}

if you extends your service:

Class ManService extends PeopleService {
    @Override
    public void pee(People people){
      if(people instanceof Man)
          System.out.println("stand up");
      super.pee();
   }
}

Class WomanService extends PeopleService {
    @Override
    public void pee(People people){
      if(people instanceof woman) //you must check it
          System.out.println("sit down");
      super.pee();
   }
}

//maybe wrong when anyone call these service
...
People p = new Woman();
...
manService.pee(p);//you can't stop it! 

than we can write a serviceFactory..., so crazy! but OOP code, People is domain object and it's perfect!

Class People {

   String name;

   public void pee(){
      System.out.print(name + " relax...");
   } 
}

Class Man extends People {
   @Override 
   public void pee() {
      System.out.println("stand up");
      super.pee();
   }
}

Class Woman extends People {
   @Override 
   public void pee() {
      System.out.println("sit down");
      super.pee();
   }
}

//we call it now, don't care about details 
...
People p1 = new Man();
People p2 = new Woman();
p1.pee();
p2.pee();

thinking of OOP basicly! Most of programer forgot it!

like image 184
Klaus Grub Avatar answered Sep 29 '22 12:09

Klaus Grub