OOP design question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeramie

    OOP design question

    The issue is having a homogenous collection of base objects, but
    needing to perform class-specific functions only on those of a
    specific type inside it.

    Cat, Dog, and Bird all derive from the Animal class
    There is an array, Animal[]
    Only Dog can Fetch().
    I need to cause all dogs in Animal to Fetch()

    It's been said that using instanceof is the sign of a bad design.
    However, my solution was basically:

    1) Go through Animal[] and create a list of instanceof Dog
    2) Go through the Dog list and call Fetch()

    Is there a better way? It doesn't really make sense to make
    Animal.Fetch() and then only Dog override it.
  • Ryan Stewart

    #2
    Re: OOP design question

    "Jeramie" <JeramieHicks@h otmail.com> wrote in message
    news:58613dc2.0 403131743.256a9 [email protected] gle.com...[color=blue]
    > The issue is having a homogenous collection of base objects, but
    > needing to perform class-specific functions only on those of a
    > specific type inside it.
    >
    > Cat, Dog, and Bird all derive from the Animal class
    > There is an array, Animal[]
    > Only Dog can Fetch().
    > I need to cause all dogs in Animal to Fetch()
    >
    > It's been said that using instanceof is the sign of a bad design.
    > However, my solution was basically:
    >
    > 1) Go through Animal[] and create a list of instanceof Dog
    > 2) Go through the Dog list and call Fetch()
    >
    > Is there a better way? It doesn't really make sense to make
    > Animal.Fetch() and then only Dog override it.[/color]

    If you have this problem, you might need to rethink your design. What are
    the other animals doing while your dogs are fetching? Should you maybe
    define a play method in Animal? In the future, post to comp.lang.java. help
    for beginner type questions or comp.lang.java. programmer for more advanced
    questions. This group isn't supposed to exist and isn't carried by all
    servers.


    Comment

    Working...