You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Yang <te...@gmail.com> on 2011/05/31 20:33:32 UTC
inheritance implementation?
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
Re: inheritance implementation?
Posted by Yang <te...@gmail.com>.
Thanks guys,
the inverted idiom is indeed easier
I'll try that
Yang
On Tue, May 31, 2011 at 1:36 PM, Doug Cutting <cu...@apache.org> wrote:
> You might invert the encapsulation, so that each Fruit instance contains
> a 'flavor' field that's a union of types like Apple and Orange?
>
> Doug
>
> On 05/31/2011 08:33 PM, Yang 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
> >
>
Re: inheritance implementation?
Posted by Doug Cutting <cu...@apache.org>.
You might invert the encapsulation, so that each Fruit instance contains
a 'flavor' field that's a union of types like Apple and Orange?
Doug
On 05/31/2011 08:33 PM, Yang 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
>
Re: inheritance implementation?
Posted by Scott Carey <sc...@richrelevance.com>.
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" <te...@gmail.com>> 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