Design question: Multiplying singletons
- From: kelvSYC <kelvSYC@xxxxxxxxx>
- Date: 24 Apr 2007 22:27:58 -0700
I've been having problems as to how to best design this, and I need
help, so here goes.
Suppose you have a singleton class, which has its own object
structure, and so on so forth. Now, referring to some unrelated part
of the object structure is easy, as you only need to get the singleton
instance and access it from there. Here's a Java-esque example:
class A {
private static A instance;
public static A getInstance();
private A();
private B b;
private C c;
public B getB() { return b; }
public C getC() { return c; }
}
class B {
private int foo(C c);
public int bar() { return foo(A.getInstance().getC()); }
}
Here, getting the C member of the A instance containing the B instance
is simple, as there is only one A instance to speak of.
Now, suppose that this singleton class is no longer singleton - ie.
multiple distinct instances exist. Doing the same thing as the above
is no longer simple, as now you have to worry about multiple instances
(ie. is this object in the object structure of this instance or that
instance?). In the above example, if A was like this:
class A {
private B b;
private C c;
public B getB() { return b; }
public C getC() { return c; }
}
Coding B.bar() would be more difficult, as there are multiple A
instances, each of which has a C instance, and finding the A instance
(assuming this is unique) that contains the B instance (in order to
get its C sibling) becomes that much more nontrivial.
So nontrivial that I am hard-pressed to find a good solution that's
sound in object-oriented design. One way is to keep an A pointer in
B, but that's high-coupling and ugly. So I'm thinking of perhaps of
an A-controller and B-controller (both singletons, where the A-
controller manages the multiple A instances and the B-controller would
keep track of the A a certain B associated to), and it seems to be
better (getting the C desired would become
BController.getInstance().getA(this).getC()). I am not sure, however,
that this is the best solution (or whether it is the recommended
solution), so I'm soliciting advice here.
This problem rears itself if you have a program that was initially
supporting one document (represented as the singleton), but now has to
support multiple documents.
Thanks in advance.
.
- Follow-Ups:
- Re: Design question: Multiplying singletons
- From: H. S. Lahman
- Re: Design question: Multiplying singletons
- From: Dmitry A. Kazakov
- Re: Design question: Multiplying singletons
- Prev by Date: Re: Modeling events that occur in a game world
- Next by Date: Re: Design question: Multiplying singletons
- Previous by thread: Modeling events that occur in a game world
- Next by thread: Re: Design question: Multiplying singletons
- Index(es):
Relevant Pages
|