Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
Like Tree2Likes

Thread: Implementation advice (instanceof and interaction between subclasses)

  1. #21
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Implementation advice (instanceof and interaction between subclasses)

    Because it models your app better.
    You have two things...a species, which can eat other species, and an actual animal, which is a member of a species.
    Your model attempted to splurge these two into a single thing. That's often the road to overcomplex code (and unecessary use of instanceof).

    You mention in your first post about people railing against overuse of instanceof, and this is a reasonable example, because it's often (not always) a sign you might have got your model a bit wrong.
    Please do not ask for code as refusal often offends.

  2. #22
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Implementation advice (instanceof and interaction between subclasses)

    Perhaps I wasn't clear, but the program isn't actually based on animals. It was just a simple analogy.

    The model is IO connectors in a connected system. IO's have a type (Input/Output), as well as several fields that define signal type and sourcefile. Input and Output extend IO, which contains most of the logic for their manipulation - connect, remove, change type, etc. These IO's are then placed on a component.

    Input/Output are separated mainly by the fact that they aren't each other. This is where I would guess the issues come in. Perhaps a flag that simply says whether it's an Input or Output, since it's relatively binary? They're a small but important part of the program, since they're used to define connections between components.

  3. #23
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Implementation advice (instanceof and interaction between subclasses)

    I'd say you'd be closer using a flag (or enum...you never know...:)) than attempting to inherit something like this.
    Without actually having the spec it'd be hard to say (and I probably don't have the time to go through one anyway).

    However, if you have something functioning that is still fairly modular (OO, whatever) and neat then I'd not worry too much about instanceof. A refactoring might be more work than is worth it. Depends what stage you're at.
    Please do not ask for code as refusal often offends.

  4. #24
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Implementation advice (instanceof and interaction between subclasses)

    I'm currently in the middle of a rewrite, which is why I asked. What I have works, but it isn't very slick.I might just leave it as is and wait for the next rewrite to look into it.

    Thanks though, you've given me quite a bit to think about.

  5. #25
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Implementation advice (instanceof and interaction between subclasses)

    One of the things to think of is that most Java stuff is best constructed via composition rather than inheritance.
    This may not be one of those things, but it's something to bear in mind.
    Please do not ask for code as refusal often offends.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. need help with subclasses
    By thorobred in forum New To Java
    Replies: 6
    Last Post: 02-19-2011, 05:34 AM
  2. instanceof
    By AedonetLIRA in forum New To Java
    Replies: 3
    Last Post: 12-01-2010, 12:34 AM
  3. Class, SubClass type (InstanceOf) Question
    By indyjoel in forum New To Java
    Replies: 6
    Last Post: 11-07-2010, 03:55 AM
  4. Use of keyword instanceof
    By darek9576 in forum New To Java
    Replies: 3
    Last Post: 03-14-2010, 10:35 PM
  5. super instanceof Class?
    By mikeiz404 in forum New To Java
    Replies: 11
    Last Post: 01-23-2009, 07:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •