Better put code in same class or in different class? [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 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!

Related

How can i keep variables state across all activities? [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 3 years ago.
Improve this question
How can i presist data across all my activities so when I change a variable on activity A, that variable will be changed on all activities to which it belongs.
So to play safe and easier I would like to know the right way to acomplish this.
I read some ppl sayng to use static variable but I have like 45-70 variables and 21 classes and doenst feels right to me to just put everything static...
I have also been using intents to pass the data between activities but I have to update it manually all the time wich can lead to inconsistencies sometimes.
My goal is to have a class that can be shared between all activities automatically and replicate any changes across all activities.
What you're looking for here is a model object. Android has a ViewModel object that would work well for this. The ViewModel object should be able to outlive the lifecycle of any activities or fragments that use it. I would recommend checking out that link as it will likely explain how to use a ViewModel more thoroughly than I ever could.
Edit: I misunderstood some parts of your question. Using a static class like you mentioned is definitely an option (and in my opinion, not an anti-pattern). If you would like to persist that data across app restarts, you could also utilize Room.
On a side note, I would also recommend using LiveData inside your model. LiveData is a wrapper of sorts that makes practically any object observable. You'll be able to observe your data in your model object and notify activities that need to know accordingly.

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)

Save and load state of GUI component [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 am new to GUI development, may be this is very easy to do, however, any help will be appreciated.
Considering that I have a GUI with 7-8 tabs, and each tab contains text boxes, check boxes and radio buttons, list-boxes and so on.
The idea is to after filling the tabs, the user can save all the data (state of the components) in a file.
And later on by loading the corresponding file, the user can retrieve all the data.
How can I able to do it without manually save all the data?
Update:
Please consider, I have the following GUI application that collect student information:
Sample application Tab # 1
As there are several tabs, the user may stop anytime ans save the information in a file, such as: Saving information
The idea is that the user can load the file later and continue his/her task.
I would like to know how I can save the state of all the components in the application and load later on.
Thanks.
You are mixing up two things here: UI and data.
The data that you save to a file should represent the "mental model" that you build your whole application around.
Example: you seem to think of a "Person" as a collection of text fields and checkboxes ... and that is wrong: a Person is a distinct class, with a set of fields respectively methods that represent behavior.
And a Person object can somehow be displayed within your UI. But what you persist are Person objects, not text fields and checkboxes!
In other words: you should first understand the object model that your application is dealing with. You learn the different ways to persist that data. And based on that, you look into providing a good graphical user experience. And when you are there, your first stop should be models - the Swing concept to "map" data to UI elements.

Implementing MVC and Observer Pattern [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 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.

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