What is the best practice to store state variables in java? [closed] - java

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 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm new to Java.
Say I want to make a simple game with Swing or whatever. How and where do I store variables like the player's score or progress, for example, so that I can access it from different classes (during the game and before persistence in the database). I like how we use useContext in React. I also used global variables in PHP's sessions.

I highly advice to not use a "global state". I would rather recommend to implement a domain object or service that fetches high scores. This service can then be injected and called where needed.
Using a domain object or a service to access and modify domain data has the benefit that ownership is clearly defined: the business object/service owns the data, and the data is only accessed and modified through the business object/service.

Related

Rest Api design: resource archetypes, store and controller [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 2 years ago.
Improve this question
i am currently in the process of looking more closely at the design of truly restful services.During my research I read something about resource archetypes at rest, four different types of archetypes come up frequently in discussion: document, collection, memory and controller. The first two I understand without any problems, but the last two I don't really understand. Can someone please give a clear explanation and example for both? Thank you.
Sure, there are four main archetypes at rest, the two that you're asking are:
Store: It'll never generate a new URI, we'll use the next URI http://api.example.com/song-management/users/{id}/playlists a user can PUT, GET and DELETE any playlist from its account but this store, the store is always managed by the client.
Controller: Are like functions, when HTTP verbs can't say the action which a resource will do, you should use this archetype. Eg. http://api.example.com/song-management/users/{id}/playlist/play will starts a playlist.
resource: https://restfulapi.net/resource-naming/

What is the most efficient way to use database? [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 using a database, specifically MySQL. I'm creating a web application for class in which you can look up the name of a book and it'll display the summary of the book. My question is should I send a query to the database that collects all of the books' data on initialization and put them into a HashMap inside a manager class for lookup or should I use a query each time to lookup a specific book information?
It depends on the data transport time I would say. If your average query time times the number of request goes faster than a script to put everything into a HashMap, use queries. Otherwise, use a script that collects everything and puts it into a HashMap.
But if you have thousands of rows, you should use queries, because otherwise you will use too much RAM.

What's the purpose of objects in Java? [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
Today I had an interview for test automation in one of the MNC.
They asked me "why do we need to create an object?"
I explained about OOPs concepts with example of individual bank account holders. But he is not convinced. He just need a definition.
What could be a suitable answer for that question?
You require an object to represent state.
At the most simple definition, a class defines behaviour and an instance of a class (an object) represents state.
Of course there are other things like static contexts which can also maintain state, which you can mention also, but above is the clearest answer which I believe they were looking for.
It also always helps to give an example. You could talk about, for example, an Employee class. You would need an object to represent John and another to represent Jane.
I think that this question is kind of generic and does not give much value to an interview. But some generic question should have a generic answer, and here is mine:
We need to create objects in java so we can get instances that have a state inside our application. This allows us to have persistent encapsulated elements that contain any required information, and methods that operate with it.
Just plain basic OOP theory.
There are many reasons why we create a object apart from basic oops
1) To bring up persistent state data to transactional state to perform action (curd and other) and persist back to data storage.(EJB, POJO,etc )
2) Creating handler to serve service and send fluid data across wire like web-service.
3)Stuctural behavior in action.for example you designed a class for a workflow and to make in action state we create a object and serve the behavior example validation , authorization , etc class
All in all to make design time architecture to response based live system

How To Exchange Information Between Classes [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 6 years ago.
Improve this question
I have a class that gets password-hash from a SQL database and there is another class which takes password from the user. How do I exchange password between the two classes? Is data access using objects follows encapsulation in java?
Ideally, you should not propagate password across layers and should use approach followed by spring-security where passwords are encrypted both in DB and from UI to server. For reference you can follow https://www.mkyong.com/spring-security/spring-security-password-hashing-example/
You don't need to implement spring-security to use this functionality but just use associated classes to achieve same behavior.
Ideally your class should get the password from the user and hash it in place.
Then it could call your DAO class passing it the hash to compare with the database to validate the user.
Passing the password around is poor practice.

Best way to maintain a list in java [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 6 years ago.
Improve this question
I have a list of objects which get created by a factory class and for the time being I want to limit the list size to 10 objects. I also need to maintain the list through out the program as multiple classes will be referencing its values. I don't want to create it as a singleton and im not happy about having a master class maintain it, so is there another way? (Best Practice)
For setting the max size you can refer to this post here Any way to set max size of a collection?
Not sure what you mean by "maintaining" the list though. There is no way (that I know of) to make that list accessible to all classes without having it in a "master class". The only other option is passing it through functions.

Categories