I have in my application three classes User
, Group
, Company
which do not belong to the same inheritance tree, as their names reveal. Each class has a constructor which receives a number of (different) parameters i.e: User(String name, String password, int type)
, Group(String name, String name)
, Company(String name, int employees, boolean isValid)
. The number of the paraameters that each constructors requires is not the same for all the classes. I have created a class ReadDataFromFile
to read some data from txt files and to create new Objects passing the data as paaraameters to the above constructors. The code of this class is apparently the same for every type, except for one method which creates the objects. Consequently it is not appropriate to create three distinct classes, but I had better to aim at a better design approach.
My question is whether the opportune design on this occasion is a Generic class, or an abstract class and implementing in its subclass the one method which differs createObject()
, assuming that the necessary data coming from the txt file are put into a String array with differnt length for each type. I would like to follow the approach of Generic class: class ReadDataFromFile<T>{}
, but I cannot find how I should handle the different types, since each one requires a call of a different constructor. Should I check for the type with instanceof
? Should I pass to the method the class of each object? Or is there a better way?