On 08/13/2015 04:48 PM, Jack Stouffer wrote:
On Thursday, 13 August 2015 at 22:49:15 UTC, Adam D. Ruppe wrote:
On Thursday, 13 August 2015 at 21:42:54 UTC, Jack Stouffer wrote:
dynamically calling different methods on each object in the list
based on its type.
The cleanest OO way of doing that is to put the methods you need in
the interface and always call it through that. Then there's no need to
cast and each child class can implement it their own way.
This really doesn't make sense in the context that I am using this code
in. The above code is a very reduced test case. In my code, the list can
have 20-30 different types of classes in it all inheriting from the same
interface, and it doesn't make sense for all of those classes to
implement a method that is very specific to one of the classes.
Enter the visitor pattern (or its variant 'acyclic visitor pattern').
Although, with 20-30 classes, it will be nasty to use.. :( So,
downcasting like suggested seems to be the best option here.
Ali