Imagine you have a collection of classes all extending a base type:
public abstract class Vehicle{
public Position position{get; private set;}
public String make {get; private set;}
public String model {get; private set;}
public void move(integer x, integer y){
position.translate(x,y);
}
}
public class Bike extends Vehicle{
public Integer speed {get; private set;}
public Decimal tireSize {get; private set;}
...
}
public class Car extends Vehicle{
public Boolean 4x4 {get; private set;}
public Integer doors {get; private set;}
...
}
In your visualforce page, you want to display a collection of different Vehicles
in a list but render html different for each type:
<apex:page>
<apex:repeat var='v' value="{!vehicleCollection}">
<div>{!v.make} - {!v.model}</div>
<div>{!v.position}</div>
<apex:outputPanel rendered="{! v instanceOf Bike} layout="none">
<div>{!v['speed']}</div>
<div>{!v['tireSize']}</div>
<apex:outputPanel>
<apex:outputPanel rendered="{! v instanceOf Car} layout="none">
<div>{!v['4x4']}</div>
<div>{!v['doors']}</div>
<apex:outputPanel>
</apex:repeat>
<apex:page>
Of course this doesn't work in VF... Is there an elegant solution out there? Or should I just give up on OO designs in apex and move to a flat model...