First, I would like to say that I think this is a common problem, and there may be a simple or general solution that I don’t know about. Many probably faced a similar problem. Thanks for reading.
I am creating a graphical interface in which each component must communicate (or at least be updated) through several other components. I am currently using the Singleton class to achieve this. Each GUI component receives a singleton instance and registers itself. When updates are needed, singleton can call public methods in the registered class. I think this looks like an Observer pattern, but singleton has more control. Currently, the program is configured something like this:
class c1 {
CommClass cc;
c1() {
cc = CommClass.getCommClass();
cc.registerC1( this );
C2 c2 = new c2();
}
}
class c2 {
CommClass cc;
c2() {
cc = CommClass.getCommClass();
cc.registerC2( this );
C3 c3 = new c3();
}
}
class c3 {
CommClass cc;
c3() {
cc = CommClass.getCommClass();
cc.registerC3( this );
C4 c4 = new c4();
}
}
and etc.
Unfortunately, the singleton class continues to grow as more communication is required between components.
I was wondering if this singleton should be used, to pass higher-order GUI components as arguments in the constructors of each GUI component:
class c1 {
c1() {
C2 c2 = new c2( this );
}
}
class c2 {
C1 c1;
c2( C1 c1 ) {
this.c1 = c1
C3 c3 = new c3( c1, this );
}
}
class c3 {
C1 c1;
C2 c2;
c3( C1 c1, C2 c2 ) {
this.c1 = c1;
this.c2 = c2;
C4 c4 = new c4( c1, c2, this );
}
}
and etc.
CommClass, , - , .
GUI, CommClass, , .
, - , . , -, . .