Design patterns to reduce coupling in Swing application

后端 未结 7 1812
刺人心
刺人心 2021-02-09 12:20

Hey all, I\'m currently working on a Java Swing application and I was looking for some guidence. The application is fairly small, but I\'m noticing that as the code base is grow

相关标签:
7条回答
  • 2021-02-09 12:58

    I don't agree with the people who suggest to use Event bus, because

    • because of code like EventBus.subscribe(SymbolListChangeEvent.class, this); your whole code will depend on a single event bus instance which makes it very hard to test,
    • it is hard to find out where a specific event is used.

    Instead I suggest to use interfaces to encapsulate external dependencies of a module. If you like, you can use them with the listener pattern, but generally are free to refactor everything if you like.

    0 讨论(0)
  • 2021-02-09 12:59

    MVC !!! Then you can use also a variant of Observer called Publish/Subscribe in order to implement the event flow inside your app.

    0 讨论(0)
  • 2021-02-09 13:06

    The best way to reduce coupling in a GUI, I think, is to use an Event Bus. There are several existing implementations out there, including some supporting Swing specifically.

    Just google for swing event bus and you'll find.

    If you use Guice in your GUI, you may also want to take a look at guts-events.

    0 讨论(0)
  • 2021-02-09 13:11

    As you already said your intent to use MVC , or you may be already using. Once you have seperated out data (I call it as data model layer). Now you need to apply OBSERVER pattern on these data model classes. All the views (your ui components) using this data model are observing this model objects for any change (Via observer pattern).

    I hope this is what you are looking for.

    0 讨论(0)
  • 2021-02-09 13:14

    Another Pattern that might be interesting for you is the MVP (Model View Presenter)-Pattern. This is great for coupling views more loosely to the model. A good explanation by Todd Snyder can be found here.

    0 讨论(0)
  • 2021-02-09 13:19

    If you want to communicate with other GUI components in the hierarchy then you should consider something like singleton that mediates calls between branches. See :

    http://blue-walrus.com/2013/06/mediator-pattern-in-swing/

    0 讨论(0)
提交回复
热议问题