Nested Classes vs Top-Level Classes

Discussion in 'Java' started by kelvSYC, Aug 18, 2004.

  1. kelvSYC

    kelvSYC Guest

    I'm writing a project where I have something like so:

    public class Container {
    Vector classAList; // a list of class A
    Vector classBList; // a list of class B
    }

    class A {
    B method() { return /* an entry in a Container instance's classBList
    */ }
    }

    where each instance of class A and class B is associated with a
    Container. (ie. a Container's classAList has all of the A instances
    that are associated with the Container)

    In this case, would it be better to have both be nonstatic member
    classes of Container, or have them be a separate class and pass a
    Container into its constructor? Should I reconsider my design?

    --
    I am only a mirage.
    kelvSYC, Aug 18, 2004
    #1
    1. Advertising

  2. kelvSYC

    Paul Lutus Guest

    kelvSYC wrote:

    > I'm writing a project where I have something like so:
    >
    > public class Container {
    > Vector classAList; // a list of class A
    > Vector classBList; // a list of class B
    > }
    >
    > class A {
    > B method() { return /* an entry in a Container instance's classBList
    > */ }
    > }
    >
    > where each instance of class A and class B is associated with a
    > Container. (ie. a Container's classAList has all of the A instances
    > that are associated with the Container)
    >
    > In this case, would it be better to have both be nonstatic member
    > classes of Container, or have them be a separate class and pass a
    > Container into its constructor? Should I reconsider my design?


    That depends. What does this program do? You have only said how you think
    you might solve a problem, but you don't say what the problem is.

    --
    Paul Lutus
    http://www.arachnoid.com
    Paul Lutus, Aug 18, 2004
    #2
    1. Advertising

  3. kelvSYC

    Chris Smith Guest

    kelvSYC wrote:
    > where each instance of class A and class B is associated with a
    > Container. (ie. a Container's classAList has all of the A instances
    > that are associated with the Container)
    >
    > In this case, would it be better to have both be nonstatic member
    > classes of Container, or have them be a separate class and pass a
    > Container into its constructor? Should I reconsider my design?


    It certainly looks like a candidate for using an inner class. Here are
    some questions to ask yourself:

    1. Which is a more fundamental concept: A and B, or Container?

    2. Do the concepts represented by A and B make sense independently of
    Container? Would A and B be logical classes to implement in an
    application where Container did not exist?

    3. Are A and B part of an interface to outside code? (If so, strongly
    disfavor writing them as inner classes.)

    Essentially, the decision to use an inner class is a decision to throw
    away an opportunity for a strong re-usable class, in exchange for the
    convenience of being able to write something with less conceptual
    weight. This is generally most justified when the potential inner
    classes are implementation details of the outer class, and not exposed
    separately as implementation on their own. Your task, then, is to
    decide exactly how much A and B are fundamentally connected to
    Container.

    Incidentally, if you do make A and B separate top-level classes, you may
    also consider employing an interface to eliminate a circular dependency
    by preventing them from assuming a Container; instead have them define
    an interface for their containing context, and have Container provide an
    implementation of that interface (which may be the Container object
    itself).

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Aug 18, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frederic HOUDE
    Replies:
    4
    Views:
    587
    Frederic HOUDE
    Aug 31, 2004
  2. pabbu
    Replies:
    8
    Views:
    711
    Marc Boyer
    Nov 7, 2005
  3. Chad E. Dollins
    Replies:
    3
    Views:
    648
    Kai-Uwe Bux
    Nov 8, 2005
  4. hemant
    Replies:
    1
    Views:
    137
    Robert Klemme
    Sep 18, 2006
  5. Markus Fischer
    Replies:
    1
    Views:
    367
    Markus Fischer
    May 31, 2011
Loading...

Share This Page