开发者

Register all GUI components as Observers or pass current object to next object as a constructor argument?

First, I'd like to say that I think this is a common issue and there may be a simple or common solution that I am unaware of. Many have probably encountered a similar problem. Thanks for reading.

I am creating a GUI where each component needs to communicate (or at least be updated) by multiple other components. Currently, I'm using a Singleton class to accomplish this goal. Each GUI component gets the instance of the singleton and registers itself. When updates need to be made, the singleton can call public methods in the registered class. I think this is similar to an Observer pattern, but the singleton has more control. Currently, the program is set up 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();
    }
}

etc.

Unfortunately, the singleton class keeps growing larger as more communication is required between the components.

I was wondering if it's a good idea to instead of using this singleton, pass the 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 );
    }
}

etc.

The second version relies less on the CommClass, but it's still very messy as the private member variables increase in nu开发者_JS百科mber and the constructors grow in length.

Each class contains GUI components that need to communicate through CommClass, but I can't think of a good way to do it.

If this seems strange or horribly inefficient, please describe some method of communication between classes that will continue to work as the project grows. Also, if this doesn't make any sense to anyone, I'll try to give actual code snippets in the future and think of a better way to ask the question. Thanks.


It is a bit difficult to judge if what you are trying to do can be solved by using Dependency Injection. With the use of the Inversion of Control principle you can, if you have some well defined interfaces inject them into the constructors of objects without for you the need to do it explicitly. It is done using a framework like String, which makes use of a container and reflexion to perform the task.

There are many assumptions on the use case like:

  • You can use interfaces to distinguish which objects register on others
  • Not sure about this one: only one object for a given interface can be added to the container

I am not a specialist for Spring but the IoC I use has the "limitation". Usually IoC is used for services and not for GUI elements, so take this answer with a grain of salt. I hope it gives you a new helpful impulse towards a good solution for the scalability you are looking for. At least the IoC provides a very good decoupling between the objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜