Nested Classes vs Top-Level Classes

K

kelvSYC

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?
 
P

Paul Lutus

kelvSYC said:
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.
 
C

Chris Smith

kelvSYC said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top