Correctly implementing the MVC pattern in GUI development using Swing in Java

MVC pattern it is a common paradigm, so there is no differences between pragramming languages in general.
However the implementation and some terminologies sometimes look different. In Java Swing it is often to see two following approaches:

1. Classic MVC

Controller – Listens user interface actions, performs corresponding Model updates. Can listen actions from different Views.

Model – Represents the state and the domain logic, methods to modify the state. Notifies listeners about the model updates (several Views can listen the updates). Model is independent and knows nothing about the listeners and their logic.

View – Responsible for user interface, UI elements layout, also listens Model updates and update the graphic inteface if required. Has some knowledge about the model, in example shown below it knows how to process list of “items”.

Design of some simple “To Do” app can look like:

enter image description here

2. MVP (Model View Presenter)

Controller acts as a Mediator between the View and the Model. View become very thin and knows nothing about the Model and interact with Controller only. Controller listens both View and Model and perform corresponding actions.

enter image description here

Swing itself adds some confusion because it uses MVC pattern for its UI components. Each UI control has a Model and View. It makes easier to design new UI components, however in a “big picture” of the whole Application design – UI controls stay on the View layer.

Leave a Comment