mardi 20 octobre 2015

Adapter class vs toXXX() method in data class

I have a scenario where I want to convert an object to a service request object type to call a service.

I have a data object like this:

class Data {
  int val;
  ...
}

I want to convert this object into a service request object lets say DataRequest. I'm thinking of these two options:

1. Have a toDataRequest() method in the Data class itself.

class Data {
  int val;
  ...

  public DataRequest toDataRequest() {
    ..
  }
}

2. Have a separate class DataAdapter, and have adapt method in it, which returns the DataRequest object.

class DataAdapter {
  public DataRequest adapt(Data data) {
    ... 
  }
}

I'm leaning towards the 1st, as it helps reducing the number of classes. I'd love to hear, what is a general recommendation for this use-case?

Aucun commentaire:

Enregistrer un commentaire