patternjavaMinor
Communication between View and Controller in MVC implementation
Viewed 0 times
mvccommunicationviewcontrollerbetweenandimplementation
Problem
I'm working on a moderately complicated (not super basic, but not a feat of programming strength) project to create a virtual tabletop for a tabletop game I play. I'm trying to conform to good design practices and patterns, specifically I'd like to keep everything as nicely MVC-oriented as possible. My problem is, I'm not certain how to nicely communicate between my view and my controller, or even my view and my components.
My view is made of custom components. Theoretically, each component would have an action/key/button handler based on its unique function, which would report to the view that an action/key/buttonevent had been fired. The View would then convert that event to something more generic (MoveEvent, AttackEvent, etc) and fire said event to the controller.
The current implementation is that the ui handlers have a reference to the view, that the view is an Observable object, and that the controller is an Observer. If I click a command button, the CommandHandler catches the event and notifies the view's observers that said event has happened.
My problem is twofold:
-
my implementation of the handlers feels wrong. I don't think I should be passing a reference to View to them, but at the same time it seems like the only other object is to make View itself the listener for all the UI items.
-
using the observer pattern for a single observer>observee relation feels wrong. I can't put my finger quite on what feels so wrong about it, but it seems like there should be a more specialized interface for message passing that keeps the decoupled nature. Do action listeners/handlers use the Observer pattern or a different method of message passing?
Here's the code (feel free to point out anything I'm doing wrong that I'm not specifically addressing in these questions as well):
View.java (you can mostly just skim this, lame ui-hand-coding except for the bits at the bottom)
```
public class View extends Observable {
private MainWindow main;
private JPan
My view is made of custom components. Theoretically, each component would have an action/key/button handler based on its unique function, which would report to the view that an action/key/buttonevent had been fired. The View would then convert that event to something more generic (MoveEvent, AttackEvent, etc) and fire said event to the controller.
The current implementation is that the ui handlers have a reference to the view, that the view is an Observable object, and that the controller is an Observer. If I click a command button, the CommandHandler catches the event and notifies the view's observers that said event has happened.
My problem is twofold:
-
my implementation of the handlers feels wrong. I don't think I should be passing a reference to View to them, but at the same time it seems like the only other object is to make View itself the listener for all the UI items.
-
using the observer pattern for a single observer>observee relation feels wrong. I can't put my finger quite on what feels so wrong about it, but it seems like there should be a more specialized interface for message passing that keeps the decoupled nature. Do action listeners/handlers use the Observer pattern or a different method of message passing?
Here's the code (feel free to point out anything I'm doing wrong that I'm not specifically addressing in these questions as well):
View.java (you can mostly just skim this, lame ui-hand-coding except for the bits at the bottom)
```
public class View extends Observable {
private MainWindow main;
private JPan
Solution
A constructor should create an object that's ready to use. It seems that an instance of
These signs point to that probably there shouldn't be an
or that it should be
and only called from the constructor.
Pass a
and either make
or move its content into the constructor and do all the necessary setup in there.
A benefit of moving the content of
And in
CommandPanel would be utterly useless without also calling its addHandler method. And it seems you never call addHandler anywhere else, except right after creating a CommandPanel instance. These signs point to that probably there shouldn't be an
addHandler method at all,or that it should be
private final,and only called from the constructor.
Pass a
CommandHandler into the constructor,and either make
addHandler method private final and call it from the constructor,or move its content into the constructor and do all the necessary setup in there.
A benefit of moving the content of
addHandler inside the constructor will be that all the JButton member variables can be converted to local variables.And in
CommandHandler, it would be good to make the view member variable private final.Context
StackExchange Code Review Q#93453, answer score: 2
Revisions (0)
No revisions yet.