• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to implement 'mutable' inheritance? Use generics?  RSS feed

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

Let's say I have a superclass, Bug, of which type my Caterpillar object is one. Great! What happens when my Caterpillar becomes a Butterfly or some other type of Bug that I haven't even thought of yet (assume I'm a geneticist)? How do I retain the benefit of polymorphism while redefining type? Use generics? If so, please describe a simple class signature?

Thanks,

Grary
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Grary:

You can't redefine type. However, you might want to consider using the Decorator pattern.

John.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or the State pattern.
 
Grary Stimon
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ranchers.

So, following the "window" example in the Decorator link helpfully provided above, if I wanted to change a VerticalScrollBarDecorator object to a HorizontalScrollBarDecorator object, I would use the former as the type Window parameter in the constructor of the latter, and I now have a HorizontalScrollBarDecorator object. Crucially, though, this changed object is still a Window implementation with all associated state.

Only problem I see here is that if I make a ton of changes, I'll have a string of past identities -- VerticalScrollBarDecorator, HorizontalScrollBarDecorator, etc. -- that can't be GC'd.

(Essentially, I'm looking to model a 'becomes-a' relationship.)

Thanks,

Grary
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Grary:

If you're making lots of changes, then Paul's suggestion of the State pattern makes more sense.

John.
 
Grary Stimon
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great. Thanks guys.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!