Forums Register Login

Teaching myself Java – how to pass on an object to an event listener?

+Pie Number of slices to send: Send
I’m teaching myself Java. I am a fairly proficient programmer in other languages, but this is the first OO language I’m doing.

I have a question that is a bit hard to summarize. Or it should be: how can I pass on an object (or variable) to an event listener?

I am writing an application in which you can play a Sudoku game. I have separated the “logic” or the “model”(the classes with Sudoku data structures and methods to manipulate them) from the presentation (the view and the controller).

The main method starts off as follows:



The first line creates class for the logic and the second line creates the class for the view and the controller. Since the view and the controller need access to the business logic, the model is passed on to the ViewController class.

The SudokuViewController class creates the user interface in Swing and it handles the user input. For the user input I have created a number of listeners, like this:



Now these listeners need access to the model since they update it. However, as far as I’m aware the only parameter passed on to an event listener is the event itself. So these event listeners do not have direct access to the model, even though it is passed on to the constructor of the class SudokuViewController.

To circumvent this, I made model2 an attribute (variable) of the class SudokuViewController. The constructor of the class sets this variable as follows:



Now the event listeners have access to model2, which they can manipulate.

This works. However, I think it is an ugly solution, introducing an additional object (model2). How can I solve my problem without doing so? I’d like to pass on the object named model to the event listener, but this doesn’t seem to be possible.
+Pie Number of slices to send: Send
You're not creating another object, you're creating another reference to the object. There is virtually no overhead involved. What you've come up with is actually very standard and is called composition. The view controller "has-a" model, so you create a reference to it.
+Pie Number of slices to send: Send
We usually discuss Listeners in the GUIs forum: moving discussion.
+Pie Number of slices to send: Send
You mean the first line creates an object from the class ... and the second line creates an object of the other class. You don't usually create classes in code. You usually create them with a text editor and now you are using those classes.

Your puzzle should be separate from the GUI. You should have classes which you create objects of which display the puzzle. Also classes whose objects solve the puzzle. The puzzle solving classes should have an interface which you can use. Another way to say that is there should be a model with a public interface. One way to do it is like this:-In my opinion the one thing you should not do is make the enclosing class a Listener. You will need a different Listener for each button and one large method is really difficult to maintain. If the Listeners are created in the constructor like that, they will have access to the sudoku reference. Or you can pass the sudoku reference to their constructors.
+Pie Number of slices to send: Send
Thanks all! Very useful information!
+Pie Number of slices to send: Send
You're welcome
You can't have everything. Where would you put it?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1987 times.
Similar Threads
passed 151/155!!!
Swing and MVC design
OO design question for game
Identifying which button in array is pressed
A little help deciding where the business logic goes?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:07:49.