Re: Design question: Multiplying singletons
- From: "Dmitry A. Kazakov" <mailbox@xxxxxxxxxxxxxxxxx>
- Date: Wed, 25 Apr 2007 09:38:05 +0200
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
.
- References:
- Design question: Multiplying singletons
- From: kelvSYC
- Design question: Multiplying singletons
- Prev by Date: Design question: Multiplying singletons
- Next by Date: Re: Modeling events that occur in a game world
- Previous by thread: Design question: Multiplying singletons
- Next by thread: Re: Design question: Multiplying singletons
- Index(es):
Relevant Pages
|