You can do this a few ways.  The composition you list will work, the member 
variable should be of type Fruit.

Or you can put the type object inside the fruit:

record Fruit {
int size;
string color;
int weight;
union { Apple, Orange } type;
}

record Orange {
string skin_thickness;
}

record Apple {
string skin_pattern;
}

However, Avro's IDL language and the Specific compiler in Java will not compile 
this into a class hierarchy.  You can use a wrapper class in Java to do that.  
A factory method to create a specific Fruit subclass by inspecting a Fruit 
would use instanceof to determine the union type and create the corresponding 
object.

One way or the other, you do need to do some instanceof / casting depending on 
what you are accessing.  I have used the pattern above, with the 'type' inside 
the outer general object.

On 5/31/11 11:33 AM, "Yang" 
<[email protected]<mailto:[email protected]>> wrote:

I understand that avro does not have inheritance now, so I am wondering what is 
the best way to achieve the following goal:

I define Apple, Orange, and Fruit. Apple and Orange should ideally derive from 
Fruit, but since there is no built-in mechanism,
we create an internal member for aboth Apple and Orange, encapsulating the 
contents of Orangle

Apple :{
Fruit: fruit_member

string: pattern_on_skin
}

Orange : {

Fruit: fruit_member

string: skin_thickness
}


Fruit: {
int : size,
string: color
int: weight
}



say I want to pass objects of both Apple and Orange to some scale to measure 
the total weight,
I can pass them just as Objects,


int findTotalWeight(List<Object> l ) {

    int result=0;
    for(Object o : l ) {
       result += ???????  <============ somehow get access to the fruit_member 
var ??
   }
}


so what is the best way to fill in the line above with "<====" ? doing a lot of 
instanceof  is kind of cumbersome


Thanks
Yang

Reply via email to