Implementing MVC and Observer Pattern [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
For a group project for school we have to make a program that implements the MVC pattern.
The difficulty I have is understanding how the MVC pattern is implemente in java with SWING, does the view make the SWING components?
Another thing I have a hard time understanding is what updates what. For instance one article says the view 'executes' the controller, then the controller updates the model accordingly, the model the notifies the view and and the view then pulls the data from the model.
While what i've learned about the MVC Pattern is that the controller has an instance of the model and the view so it can interact with both. but then if I push a button in the view how does the controller know to do something with the model? And for this example which clas(ses) is/are the Observer(s) and which clas(ses) are the Observable(s)?
Another idea I had is that if we use the MVC Pattern as described above the view is the Observer and the model is the Observable, but that still leaves the question how does the controller know when to update the model?
If it seems vague please ask for clarification in the comments and I will answer.

The View communicates with the controller, the controller communicates with the model and vice-versa.
Say you have a model class Graph that contains the points of a graph. Your views purpose is to display that graph. The Graph class would be an Observable that the controller observes. If the controller observes change, it will tell the view update/change accordingly. If you press a button on the view, say to display a different graph object, your button has an actionListener that asks the controller to change the graph.
And yes, your view classes would create the Swing components.

Related

Better put code in same class or in different class? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm newbie programming in java.
I'm doing some forms and in one of them I put some fields, a button and a JTextArea. The idea is when I click in button makes a connection to external database and in JTextArea shows me the connection return (if is okay or if is failed and the error).
So I've created a class with the UI and a class(connection name) with the attributes database, port, username, password ... that make connection to external database.
I don't know if these organization mode is the best way to make it. I should include class connection in the same class of the UI,? How should I pass values of the form to the class connection, by a method?
Could you suggest me how to make it?
As lealceldeiro commented, this is opinion based, but there are some structures that it is good for these types of applications. What I've been taught and have been using is MVC(Model, View, Controller) structure.
The model is responsible for managing the data of the application. It receives user input from the controller.
The view means presentation of the model in a particular format.
The controller responds to the user input and performs interactions on the data model objects. The controller receives the input, optionally validates it and then passes the input to the model.
I usually make packages/modules called controllers, views and models. So I think you're on the right track here.
Let's say you have one class for your GUI, where the button onClickListener is. That would be your View.
Your models would be the class that you use to connect to the database, and maybe some classes/models in which you store the data from your database queries.
For you I would suggest to make another class that will be your controller. This class would contain your database object, and you can use this to make queries to the database and update your models. One of these could be connectToDatabase(), and you could call this method from your GUI when the button is clicked.
I hope I explained this well enough, and good luck!

Activity or Fragment as View on MVP [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to know which is the better implementation of the view on MVP.
For example in small Apps, It's better to use one or two activity's and use as a View the Fragments? or It's better use one Activity for all screens as View without any Fragments?
Whether you should use activity or fragment not actually depends on MVP. This decision should be taken based on your app's requirement. If the app is such that the pages do not have much to share between them and pages are quite unique based design and functionality, you should go for activity. On the other hand, if there are lots of interactions going on between the pages or based on design and functionality lots of similarity is found between the pages, using fragments will be a better option. It will reduce lot of duplicate codes.
One definition of MVP (there are quite a lot) states, that the presenter is an implementation detail of the View. So, MVP per se has no preference for either of Activity or Fragment. You can attach a presenter to both of them equally well and use it to separate business logic from the presentation layer.

Where should I perform Retrofit operations? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm new to MVP, Retrofit and Dagger, so I've decided to make a simple app based on Riot Games API, that just shows some game's info in a list. Everything works fine, but it made me wonder: "Is presenter a good place to do such things, like Retrofit operations? I couldn't find anything about it. There are some simple examples how to use Retrofit, but they are sometimes shown even inside an activity class, which doesn't seem right to me.
So, here is my question: Where should I perform such operations to make the code as clean as possible?
My app repository: https://github.com/Mallorax/Rito_Api_Test
If the Model is what Retrofit returns, you can't put the call there.
The View is intended to separated from the Presenter, and subscribes via Callback interfaces, which Retrofit provides out of the box.
Therefore, your only option without involving some Service layer is to put Retrofit in the Presenter.
I suggest you to place some network call to plain class e.g. Interactors for example LoginInteractor which contains loginMethod, than when it finishes pass the result to the presenter class, which will handle the data and will convert (or not) it for some kind of model which is ready to be presented on UI.
Activity/fragment contains Presenter and Presenter contains Interactor(s)

Making the user follow specific steps [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Am not sure if the title describes what I want. Am developing an App on Android where kids start a new empty drawing project and finish with a full drawing at the end by dragging different shapes from a list of shapes that I created for the App. My problem is that I dont know how am going to follow the kid steps.
The kid is supposed to read a message in each step and try to apply it and then click on a next button to view the next step.
I want to make sure that he do exactly what the message is saying in order to view the next step.
Theoretically, how is this applied in Java ? If for example, there would be 20 steps how can I make the app follow up with him and make sure he is doing the right thing
You are looking to create a wizard. In Java use a MVC libray Spring-MVC or if its android you can use similar approach. Basically you want steps and views while saving the state - typical MVC pattern. Have a model object that is passed along to the views. Each view will have portions of the model that it can read/write to.
Android -refer to WizarDroid.
Spring - AbstractWizardFormController will help

Java MVC with File Input/Output [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am creating a program using MVC pattern in java... However Im very confused on how to properly perform file input.
For example, should I perform all file input in MAIN? ..I tried but its getting messy, and Im not sure if there is a "uniform" way of performing file input for MVC pattern. This is for an OOP assignment btw. Thank you.
For a theoretical question, the answer would be theoretical.
MVC means Model View Controller
Model - Model represents the layer that is closely related to data. Say if you want to persist a Employee object, you have a Employee POJO carrying the Employee data.
View - View represents the visualization of the data that model contains.
Controller - Controller acts on both Model and view. It controls the data flow into model object and updates the view whenever data changes. It keeps View and Model separate.
In your case, File Input process - what do you mean by that? A File is send as input and you need to persist the file in your database?
This is not a good use case for showing MVC pattern. yet..for your understanding
View - you can have a File Upload page
Controller - The class that route the control from view to a Java class that performs File Read, and performs business logic on the input file
Model - A class that has database connection setup, and has a method that accepts the final formatted File and store to the database.
More experienced folks, correct me if you feel that way

Categories