I have many T arrays to read. Each T array can represent data of type A, B or C. A: A list of T B: A single T C: Exactly three T
When I read a T array, I will be able to get a list of T, and determine by reading the first T in the list whether it's of type A, B or C. I thought of two possible approaches and would like to know their merits and cons.
1)
enum {A, B, C};
class X {
enum dataType;//A, B or C
List<T> data;//just store all as list of T
//other essential methods/properties
//throws exception if an instance cannot fit into either A, B or C.
X(T[] input)
{
//read input
}
}
2)
abstract class X
{
abstract int length{get;}
//and other common essential methods/properties
}
class A : X
{
List<T> data;
}
class B : X
{
T data;
}
class C : X
{
T data1, data2, data3;
}
class ConcreteX: X
{
List<T> data;
ConcreteX(T[] input)
{
//reads input and stores T(s) into data
}
}
class XFactory
{
getX(T[] input)
{
ConcreteX conX = new ConcreteX(input);
//depending on whether it's A, B or C, create an instance of A, B,
//or C and map the corresponding fields from conX to the instance.
//return that instance
}
}