Design question: Multiplying singletons



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.

.



Relevant Pages

  • Re: Updates to a single class instance
    ... I tried to use a singleton, ... public class Collector{ ... private static Collector ref; ...
    (comp.lang.java.programmer)
  • Re: globals?
    ... singleton http://en.wikipedia.org/wiki/Singleton_pattern. ... Public Module myGlobals ... Those are therefore private to the module, ... myglobals.instance.somedata="Test String" ...
    (microsoft.public.dotnet.languages.vb)
  • Re: globals?
    ... And it is not a singleton that word is not used in the VB language. ... Public Module myGlobals ... Those are therefore private to the module, ... myglobals.instance.somedata="Test String" ...
    (microsoft.public.dotnet.languages.vb)
  • RE: Singleton / Function
    ... The singleton pattern uses a private constructor a public static method and a ... private static instance of itself to pass back from the method. ... interface method, but I am not sure you need an interface for what you are ... > static ILocalPersistence* GetLocalPersistence; ...
    (microsoft.public.dotnet.framework)
  • Re: Shared globals between two classes
    ... I realize that public data ... Little change needed except for marking them 'private' and providing ... int getTXCount() const ... I recognise the Singleton structure may be somewhat confusing but please ...
    (alt.comp.lang.learn.c-cpp)