mardi 15 décembre 2020

create object (a) from other object (b) data depends on (b) type

I have `

class I1 {
 private int line_num;
 private String text;
}

class A extends I1 {
  private int additional_data_for_a;
}

class B extends I1 {
  private int additional_data_for_b;
}

class C extends I1 {
  private int additional_data_for_c;
}

and I also have the reflection for these 3 classes 

class I2 {
 private int line_num;
 private String text;
}

class X extends I2 {
  private int additional_data_for_x;
}

class Y extends I2 {
  private int additional_data_for_y;
}

class Z extends I2 {
  private int additional_data_for_z;
}

`

I want to create X from A, Y from B, and Z from C,

I also have some logic to convert from additional_data_for_c to additional_data_for_z it's not just passing the value.

the question is what is the best way to create these objects and using design patterns? I have nieve solution which is using instansof then call the related constructor but seems like not a good approach, I was thinking about making the constructors for X, Y, and Z receiving I1 instance and do it in the constructor, but I'm not sure if this is a good approach, Is there any design pattern to solve such problems ??

Aucun commentaire:

Enregistrer un commentaire