mvc without view - controller connection - java

I know how classic MVC looks like, but I was asked to write game using other type of MVC. I mean something like this : Draw which show my lecturer
BQ is LinkedBlockingQueue of Events. And I don't know how in this situation model can tell view and controller that his state has changed? My antoher problem with this MVCis fact that Controller shoud make deccision about consequenses of Clicked Button, but again, there is no connection from view to controller. Is it means that View should implement ActionListers inside of it?

It seems to me that the diagram shows the view generating events and placing them on a queue. The controller is reading from the queue and updating the model/notifying the view accordingly. To me this is still MVC.

Related

ViewChangeEvent not triggered in a view that is contained in a TabSheet

In my Vaadin 8 application, I have a TabSheet, that contains several views. When I override the enter method in the first view of my TabSheet, and then I proceed to enter the view on my application, the method doesn't get called.
Do you have a Navigator, and have you registered the views for it? Navigator's navigateTo is what calls the enter. Registering views happens along these lines:
navigator.addView("", new InitialView());
navigator.addView("second", new SecondView());
Navigation also updates the URI fragment in your browser, and makes it possible to bookmark specific views and enter them via direct URL. See e.g. https://vaadin.com/docs/v8/framework/advanced/advanced-navigator for more information about Navigator.
TabSheet isn't the most trivial thing to use with Navigator and I'm afraid I don't have a ready-made example at hand, but I think it should be doable with a custom ViewDisplay and maybe SelectedTabChangeListener.
If you aren't interested in the Navigator approach, I suppose you could replace the View+enter with something along these lines, although if you need to know the previously selected tab you'll need to keep track of it yourself since this particular event isn't very informative:
tabSheet.addSelectedTabChangeListener(e -> {
((MyClass) tabSheet.getSelectedTab()).myMethod());
});

How to construct the RecyclerView for the following UI vertically?

The expected UI for my application should look like this:
I have thought about using a view pager but I cannot figure out how to change opacity of the next items or make the item in view to be centered and be elastic.
I have researched multiple open source GitHub repos and found two which seems similar to what I have approaching to do.
https://github.com/bloderxd/deck
https://github.com/Ramotion/garland-view-android
I do not know how to solve the problem of opacity or going to the next item using the next button in the UI (which was designed by me for a project).
So far, I could only create a simple XML representation of the UI without the view pager but I do not know what other libraries and views I need to implement to make it interactive and elastic like in the UI.
My request is how I could build the application level code for this UI. If there is any other obscurities in my questioning, feel free to comment and reach out because I am relatively new to this kind of development. Thank you.

Changing Views in Java using MVC model

So I have read a lot about MVC online and have learned about it in class, but I am still lost on one aspect - changing and showing Views. I know Views are GUI, they pass user input to the Controller, but I'm having a hard time wrapping my mind around how the following would work:
View A displayed
user clicks button on View A
Controller notified, tells Model
Model tells Controller to display View B
Controller displays View B?!?
The last 2 lines here is what I don't understand how to implement. If the View did not change to another View, I know to use the Observer/Observable interface to update the View. But in my case there is a Home Screen and a Game Screen and when the user clicks Play button on the Home Screen, I want the "view" and the GUI to change to the GameScreen. I want to use 2 distinct Views (I think).
I'm having trouble structuring my code to achieve this, and I don't know where to put the ActionEventListeners
Assuming you're just switching the view, this is the sequence.
View A displayed
User clicks button on View A
Button controller tells view to display View B
View displays View B
The model is not involved at all. Other controllers can change the model.
When coding a Java Swing application, here's what I do.
The view may read values from the model.
The view may not update the model.
The controller(s) will update the model.
The controller(s) may revalidate / repaint the view.
To see an example of the model / view / controller pattern in a realistic Swing application, take a look at my article, Retro Snake Game.

Java View to Controller Observer

I need help. I am struggling to get my Observers working in java. Can someone explain to me using MODEL-VIEW-CONTROLLER Architecture how to create and observer from View To Controller.
This is because if i press a button on the view the action event has to call notify the controller of that button being pressed.
For that I'm implementing observers to minimize class coupling.
I have a class Controller, View (Swing using JFrame), and an Application Class that holds the main method.
I tried implementing it so that Controller implements Observer and the View extends Observable.
After triggering the event of clicking the button all code except the notifyObservers("OBJECT") gets called. It disappears somewhere in the java library.
Any Help Will be much appreciated.
the model should extend observable and the view should implement observer (you want the view to depend on the model). you will need to call setChanged to after you change the state of the model to force the observers to be notified.
Double check, that your controller is really observing/listening to the (correct) button instance. Use a debugger and set some breakpoints to check whether notifyObservers is called and who is receiving the notification.

Decoupling View from Controller in Java MVC Pattern

First time posting a question on StackOverflow, so please go easy on me :)
From what I understand, proper use of the model-view-controller pattern requires that we decouple the view and controller such that the view knows nothing about the controller. I'm having a bit of a problem understanding how to do this using Java Swing.
Say I have a view (some class that would extend JFrame), and this view has a button. Is it safe to say that I would want to register the controller as an ActionListener of the button? Or do I make it a listener of the entire view itself.
And how do I go about doing this without doing something like:
button.addActionListener(myController)
in the view, because if I were to do this in the view code, wouldn't it now have a dependency on the controller?
I didn't post any code because, frankly I don't have much to go on at the moment.
any help is appreicated!
It might help to not think of the view in terms of buttons etc. so much as an interface. The interface makes it possible for web ui's, command line consoles, etc. to be written and fulfill the role of the view.
In the case of your button event, the button represents a call to some command carried out by the controller.
So, you could have an interface like this:
public interface MyViewIf {
// used by the controller to register its self as a listener of the view
public addViewListener(ViewListener vl);
...
}
and:
public interface ViewListenerIf {
// used by the View to notify any listeners of control events etc.
public onViewEvent(ViewEvent ve);
}
Then your controller would implement ViewListenerIf and register it's self with a factory generated instance of MyViewIf. That way the controller doesnt need to know any specifics about your view class(es).
Your view class would then internally handle it's own button events, turn them into ViewEvent objects and call onViewEvent() on the controller that registered it's self with the view, leaving the View 100% oblivious to the existence of the Controller.
Make an actionlistener in your view class. From your actionlistener you call your controller.
Code for actionlistener:
controller.doButtonClick();
This means you need to inject your model and controller to the view. That is how I do it.
Take a look at the Spring Framework to get an insight in implementing the MVC pattern.
Brief Spring tutorial Tutorial

Categories