where to store data read from file [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 1 year ago.
Improve this question
I am making a semester project for my University OOP course. I am making a Restaurant Management System as my project.
I have an Order class, Main class, and my constants stored in a class called GlobalConstants.
I read in a post on this site that said constants should be in related classes. In GlobalConstants I have a method running in a static block that reads order data (order no., time, items etc.) from a file and stores it in an ArrayList (static and final) which is accessed by multiple methods in the Main class.
So, can anyone suggest a better way to do this that follows OOP practices?

First, don't create any logic in a class, called GlobalConstants. This is the Separation of Concerns principle.
This made clear, you could:
First: create a util class e.g. FileUtil where you will move your static method. It wont store the result in a field, but will return the result instead (or alternatively take the list as an argument and populate it). Storing (if needed) or whatever other work with the result is needed is the job of the class that is calling the method, not of the util.
Second: I suppose that you have to store the content of your file in memory in order to do something with it for your order. You must decide what is the scope of this data.
If these are some global settings to your applicaton comming from this file - you could easily create a Settings class that will be Singleton (find info and read about Singleton - this will be a good start to get known with Design Patterns).
If it is some session data - maybe the order itself is coming from a file - then store this data in your Order class - this is its purpose after all.

Related

OOP encapsulation based on functionality of a program? [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 2 years ago.
Improve this question
I'm learning Java and the basics of OOP. Imagine I'm making a Person class. A person has of course a firstname and a lastname, that's why I declare two private variables in the Person class, being firstname and lastname. Both getting their initial value in the constructor. Now, how do you decide wether they both need a public getter and a setter, or only a getter and not a setter? Do you take this decission based on the kind of functionalities your application should have?
For example if you are building an application for the local sportsclub to keep track of their members (each member is a person object). Within the application there is a button to create a new member. if you push this button you have to fill in a firstname and a lastname and push the button 'create and add to club'. Behind the scenes there is a person object created. The application can show a list of all members and delete a member and thats all it can do.
Now there is no functionality like change firstname or lastname of a member. Could this be a reason why I should not have a setter for firstname and lastname? So if I created a member maked a typo in his firstname, I first have to delete him and then create him again without the typo. If the application had a button 'change name' I should need a setter or a method like changeName or something because I want to alter an already existing object.
Is this the correct mindset or has functionality nothing to do with encapsulation?
Ps. I know it's simple example but it's just to base my question on.
Thanks
You make that kind of decisions based on the character of attributes. Are they public (used by external to the object/class entities?). Are they private (for object/class internal use only?). Should they be read-only after construction? When they are read-only "to the public", you don't need a public setter. If internally you still want to change them, you probably can access them directly without going through a setter.
It also makes sense to be able to fix an erroneous attribute without having to destroy the object and recreate it.

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

Containers and method packages in software architecture - how about static? [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 8 years ago.
Improve this question
Recently I am working on an applications (in Java and C#, but I think the problem is not closed to those languages) where I use a few container classes (which responsibilities are storing data in a proper order) and services being method packages, operating on data stored in container classes. All of the classes mentioned above should have only one copy existing in memory, and all of them are expected to be in the memory for the whole time the application is running.
I used to think that a singleton is a good idea here, as I am sure there is only one instance of each class, so it meets my expectations. However, I learned that the Singleton pattern is deprecated, as it hides dependencies and so on. Then I heard that for such usage (always available container class or method package) static classes may be a good idea. On the other hand I recently looked at a few projects where people refused to use any static stuff, as if it was an awful practice to do so.
My question is simple (at least in its formula): are static classes a good idea for creating always available, easy to hanlde containers and method packages? If not, what should I use instead (if not singletons)?
You don't really say where the data comes from. If the data is static, then a static class is a fine solution. For example, I could envision a static class to represent the 50 US states.
In contrast, for a class that represents a list of authorized users, I would use a singleton pattern. Although there is only 1 list, that list could change while the app is running.

Is it better to create one instance of a class or make an individual instance for every occurance? [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 9 years ago.
Improve this question
I've been hunting for tips on good Java coding practices, by looking at the code of accomplished programs. My first target was Minecraft, since I'd tried my hand at modding it, and I started to question my choice. Here was code from an accomplished game, and it was giving me two very different ways to go about things.
For those who don't know, Minecraft instantiates its items once and subsequently references that single instance and its methods for any operations it needs to carry out, using information from other sources for the method parameters. Its entities, on the other hand, are instantiated once for every individual entity in the world and are responsible for their own information.
So, the crux of the issue is: Which method is more efficient? Is there a particular reason to favor one over the other? Is it situational? Is it more efficient to do it one way or the other?
The answer is, in most cases, it depends.
What you describe is the singleton pattern, which there's one and only one instance of an object. This is beneficial if having more than one instance is either expensive (such as multiple instances of a DAO), or doesn't make much sense (such as multiple instances of a DAO).
Individual instances of objects is necessary if you hold two separate, distinct instances of the same class - for instance, say you're holding two diamond pickaxes. I wouldn't imagine that a singleton would make sense in that context, since you can interact with each pickaxe individually.
Use the pattern most suited for the situation. There is (and won't ever be) any one-size-fits-all way of solving problems like this.

Strategy pattern that accesses the user's members [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
I have some trouble finding appropriate patterns for what I want to do.
I have a block game with two game modes. In one mode, any removed blocks are replaced by new blocks dropped from the top of the screen. In the other mode, removed blocks are replaced by a complete new row of blocks rising from the bottom and pushing the whole field up. So I thought it would be best to use the Strategy pattern to implement this.
Now the problem is, that this Strategy would need modifying access to all the positions of existing blocks in the playing field, but obviously that information lies within the Strategy's user class (the Game class) and I can't have circular dependencies.
I could pass the whole field as a reference, but I have the additional problem that all the Block generation code lies within the Game class (as it should in my opinion). So the only thing that comes to mind is pass all these methods as function references, but to me that seems like overkill.
So any way to resolve this? Maybe I'm on the wrong track by wanting to use the Strategy Pattern. Help is greatly appreciated.
Bonus points for any hints regarding the use of two Strategy Pattern objects in a class that need access to the same methods which depend on members in the user class.
I'm coding in Java if that is of help
I guess in the end it was kind of too complex to assume an out-of-the-box answer.
I kind of followed Traxdata's hint of decoupling more together with Mister Smith's suggestion to actually pass references to other objects.
My final solution consists of having a FieldManager, a Field and a Strategy class. The FieldManager contains a Field and Strategy instance. The Field class contains the block creation code.
So whenever blocks need to be created or removed, the appropriate function calls the corresponding Strategy's function and passes a reference to the field. So whenever a Strategy needs to create or remove something in the field it can do so via the field object.

Categories