Re: Design question: Multiplying singletons



On 24 Apr 2007 22:27:58 -0700, kelvSYC wrote:

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.

It is the coupling you have introduced it per design: A knows B, B knows A.
So it is too late to complain. I would try to redesign it in order to
separate containment relationships from functionality of the elements.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
.



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)