How i would create multiple instances of different classes in an easy way?

1.5k views Asked by At

I'm searching a way to creating different instances of different classes without writing a hacky code like:

PersonInstance pinstance = new PersonInstance();
FamilyInstance finstance = new FamilyInstance();

But with doing this it can go too far numbers of instances (like 70). Any ideas on how to make a good code without writing 70 lines that having the same sort of codes? If you didn't understand:

Initializing 70 different instances (different classes, everything different) with the classic method

PersonInstance instance = new PersonInstance();

for 70 times (different class, different instance identifier) is a bit hacky. I would like to know how to initialize them without using that way. Clearly all the classes have something common thing that they all extends on the same class. I want to know if it's possible or not?

5

There are 5 answers

0
AmirHossein SayyadAbdi On

If I understand this correct, you have to create a superclass for all your different classes. Example:

class B extends PersonInstance
{
//
}
class C extends PersonInstance
{
//
}

Then, you have to create an array or a list for example:

List<PersonInstance> list = new ArrayList<>();
for(int i = 0; i<=69; i++)
{
    list.add(new B());
    // or list.add(new C());
}
0
bilelovitch On
0
Michail Michailidis On

You could use Dependency injection/Inversion of Control (IoC). For example Spring comes with annotations like @Autowire to put over your constructor.. The Dependency Injection Container does all the initializations parsing all the dependencies and passing them to the proper constructors without the need to call new yourself . The code becomes cleaner and easier to Unit Test..I will add links later when I am on a PC :)

5
CJR On

Instead of doing this:

PersonInstance instance1 = new PersonInstance();
PersonInstance instance2 = new PersonInstance();
...
...
...
PersonInstance instance99 = new PersonInstance();

You could use an array to create all 70 instances of PersonInstance, like:

PersonInstance[] personInstances = new PersonInstance[N];
for(int i = 0; i < personInstances.length; i++){
    personInstances[i] = new personInstances();
}

Where N is the number of slots in the array where you could store instances of PersonInstance. In your case, N would be 70:

PersonInstance[] personInstances = new PersonInstance[70];
for(int i = 0; i < personInstances.length; i++){
    personInstances[i] = new personInstances();
}

Then if you would like to access PersonInstance number k, you would do so by:

PersonInstace thisPerson = personInstances[k-1];

since an array start from index 0, e.g if you want to access PersonInstance 56, you would do so by:

PersonInstace thisPerson = personInstances[55];

Hope it helps.

UPDATED ANSWER:

If for example, BrotherInstance extends PersonInstance, you could add a BrotherInstance to your PersonInstance array like:

PersonInstance[] personInstances = new PersonInstance[70];
    //add 50 PersonInstances
    for(int i = 0; i < personInstances.length-20; i++){
        personInstances[i] = new PersonInstances();
    }
    //add 20 BrotherInstances
    for(int i = 50; i < personInstances.length; i++){
        personInstances[i] = new BrotherInstances();
    }

And access them one by one like:

PersonInstance currentPerson = personInstances[24]    //A PersonInstance
PersonInstance currentBrother = personInstances[61]   //A Brother instance

or

BrotherInstance currentBrother = personInstances[61]   //A Brother instance

or loop them all like:

for(PersonInstance person: personInstances){
        //Do something with every person here, maybe print out something.
    }

That for-loop will loop through all PersonInstances, wether they are PersonInstance or BrotherInstance since they are either a PersonIntance or extend PersonInstance.

1
skag On

If the classes are interrelated i.e inheritance then make an array. Visit C++ create array of objects (from different classes)