0

Assume that I have AnimalData and AnimalListData:

<bean class="com.chang.data.AnimalData">
    <property name="name" type="java.lang.String"/>
    <property name="weight" type="java.lang.Integer"/>
</bean>

<bean class="com.chang.data.AnimalListData">
    <property name="animals" type="java.util.List&lt;com.chang.data.AnimalData>"/>
</bean>

If I create ZooData with a list of AnimalData, is it a better to use a List of Data objects (Approach #1) or a ListData object (Approach #2)? When should I use Approach #1 or Approach #2? How about for WsDTOs? Is the case the same?

Approach #1:

<bean class="com.chang.data.ZooData">
    <property name="animals" type="java.util.List&lt;com.chang.data.AnimalData>"/>
</bean>

Approach #2:

<bean class="com.chang.data.ZooData">
    <property name="animals" type="com.chang.data.AnimalListData"/>
</bean>
geffchang
  • 3,279
  • 2
  • 32
  • 58
  • 1
    Since it's DTO/Data objects, it doesn't make much difference here. I would prefer Approach #1, which is easy to get the list of objects. – HybrisHelp Mar 05 '20 at 04:46
  • Why would you create a class to keep a `List` state **only** ? In my opinion it's not a good approach whatever the situation is. Approach #1 is the best solution. – user1234SI. Mar 05 '20 at 21:53

2 Answers2

1

It doesn't make a great deal of difference in my opinion, so some of this is personal preference. The thing I would think about is: in approach 2 you have to do zooData.getAnimals().getAnimals(), whereas in approach 1 it is zooData.getAnimals(). The same applies for the DTOs point. So approach 1 seems more intuitive to me .....

1

Approach #2.
Think from a real-world mapping perspective. Do you actually have a real-world object AnimalListData?
AnimalData, on the other hand, is actually a real-world object that can directly be mapped to get detail of the animal.
The list is just an abstract construct that you have created to simplify your programming otherwise it does not exist.

Farrukh Chishti
  • 7,652
  • 10
  • 36
  • 60