Note that there are some explanatory texts on larger screens.

plurals
  1. POBetter alternative to downcasting?
    primarykey
    data
    text
    <p>What would be a good alternative pattern to this...?</p> <p><strong>NOTE</strong>: I'm keen to avoid downcasting the objects every time I want to access the sub-class' methods, ie the <code>((Bicycle)vehicles[1]).getSaddles()</code> bit. The sub-classes of <code>Vehicle</code> are only going to be simple with a constructor that takes in values and some getters, but the getters are not the same.</p> <pre><code>public class Main { Vehicle[] vehicles; public static void main(String[] args) { new Main(); } private Main() { DataStuff data = new DataStuff(); vehicles = new Vehicle[data.getNext()]; int i=0; while(i&lt;vehicles.length) { int type = data.getNext(); if (type == 1) { vehicles[i] = new Car(VehicleType.car, data); } else if (type == 2) { vehicles[i] = new Bicycle(VehicleType.bicycle, data); } i++; } if (vehicles[1].type.equals(VehicleType.bicycle)) { System.out.println(vehicles[1].type.toString() + ", number of saddles:" + ((Bicycle)vehicles[1]).getSaddles()); } } public class DataStuff { private int[] data = new int[] {2, 1, 4, 5, 1, 4, 2, 2, 1, 1}; private int pointer = 0; public int getNext() { return data[pointer++]; } } } </code></pre> <p>...</p> <pre><code>public class Vehicle { public VehicleType type; public Vehicle(VehicleType type) { this.type = type; } } </code></pre> <p>...</p> <pre><code>public enum VehicleType { car, bicycle } </code></pre> <p>...</p> <pre><code>public class Bicycle extends Vehicle { private int wheels; private int bells; private int saddles; public Bicycle(VehicleType type, DataStuff data) { super(type); wheels = data.getNext(); bells = data.getNext(); saddles = data.getNext(); } public int getWheels() { return wheels; } public int getBells() { return bells; } public int getSaddles() { return saddles; } } </code></pre> <p>...</p> <pre><code>public class Car extends Vehicle { private int wheels; private int seats; private int engines; private int doors; public Car(VehicleType type, DataStuff data) { super(type); wheels = data.getNext(); seats = data.getNext(); engines = data.getNext(); doors = data.getNext(); } public int getWheels() { return wheels; } public int getSeats() { return seats; } public int getEngines() { return engines; } public int getDoors() { return doors; } } </code></pre> <p><strong>UPDATE 1</strong></p> <p>Assume that the objects ObjectA and ObjectB are completely different(ish) but I must store them in a generic way. ObjectA could be a definition of a country, ObjectB could be a tree...</p> <p><strong>UPDATE 2</strong> </p> <p>(I also updated my psuedo code above to fix an error and make it more obvious) I was hoping not to write too much code as I don't want people getting bogged down with the specifics of my test case, but the code above is close to what I need to achieve.</p> <p>My question was perhaps poorly worded. I'll try to be less vague.</p> <p>So... I have data being read in regarding arbitrary objects in a random order that DO share properties (my code above doesn't reflect this though). I need to be able to iterate through the objects after parsing the int[] and get the specific data for each too... I want to avoid, however, all the downcasting that I would have to do (example in the <code>println</code> statement).</p> <p>What is a good alternative pattern to be using? Or is this going to be ok? I don't have to have huge if else blocks as the data in the int[] (don't worry about the DataStuff class that is just a simple example) will all be generated based on a query (also not shown in the above code) that will be provided by the implementation, so the dev will know what he is expecting back (by developer I mean me anyway lol...).</p> <p>I hope that this is enough info to describe what I'm trying to achieve.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload