Background -
I have an app that currently has a lot of methods in MainActivity to pull and upload data to firebase. It's quite a mess with the other app logic in there..
Problem -
I would like a separate class filled with server (firebase) access methods that can be called from any activity or fragment and run asynchronously. The class also needs access to context and be able to initialise and store variables.
PS. I'm using kotlin if that helps at all
Edit:
Have done more researching to find terms like "utility" and "static" classes which seems like an accurate way to go... If I create a static utility class with Async methods, will that achieve what I'm after? Also, can you initialise and hold variables in a static class?
Thanks :)
Solved
After more research and testing, I created an object that holds my methods and variables and just need to pass context into the relevant methods. Then just simply call objectname.methodname()
I am sorry if this question seems pretty basic and there is already an answer for that, but unless I make it into a proper question, google won't find it.
I have a main class that is a JFrame(there is and will be only one object, let's call it "main"), it creates and calls another JFrame class(let's call it window2), however, I still need this window2 to call methods from the already existing main class. Normally window2 would have something like Main mainMenu = new Main();.
But this obviously creates a new object of main, I still want to refer to the already existing object and get information from it.
Dependency Injection. The answer depends on whether your Main class is static, but let's assume it is not:
Create a Main reference in Window2: Main main;
Create a method in Window2: public void injectMainInstance(Main main){this.main=main}
In Main you have the Window2 instance window2. Call window2.injectMainInstance(this);
You should be good to go now in Window2 with main.mainMethodTBUsed();
I'm creating simple game engine in Java and I've got some packages like a:
Game
Input
Time
Graphics
Each package handles a lot of classes, most of them have (And should have) public access. Let's focus on one most important class called MouseInput.
MouseInput class handles ONLY public static methods like a getMousePosition(MouseAxis axis) {...} but it also handles some methods like a updateMousePosition() {...}.
And now I want to make this method (updateMousePosition()) callable ONLY by GameBase class that is inside Game package.
P.s. I don't want to put all those classes in one package! I want to separate them to don't make my project messy.
2th P.s. All those methods that I want to make callable only by GameBase are static.
It is possible to restrict who can call even public method by checking the call stack. This adds some code to the method receiving the call but any "wrong" calls can be rejected.
I have implemented a GUI using javafx in a class called "Gui" which extends apllication. I have a seperate class which handles the logic called "Logic". I want to pass an instance of "Logic" class to to "Gui" class. Is there anyway that I can create an instance of "Gui" class before calling "Application.launch()" in main method?
Not easily, and this is almost always the wrong way to approach this. In particular, you don't even know that the main(...) method will be invoked: in Java 8 a JavaFX application is launched by the JVM without (necessarily) calling main(...) at all.
You should really consider the start(...) method in your Application subclass as the equivalent of the main(...) method in a regular Java application; in other words, think of this as your application entry point and create the Logic instance there.
I have a GUI class with a menu of buttons and textfields. Depending on what choices that is made in the menu and the input, methods in the GUI class are calling methods in the Logic class to send the input and create new objects of Customer class and Account class and so on.
To be able to communicate between the GUI- and the Logic class, I first create an object of the Logic class and I do that inside the GUI class, since it's here I have my main method. It this the best way to do it? Do I need some kind of reference variable between GUI- and Logic class or just use the reference when the object was created in the beginning of the GUI class? I guess to be able to communicate with a class, it must be an object first!? Thanks!
Logic logic = new Logic();
logic.addCustomer(name, number);
Ideally you shouldn't directly create the logic class.
You should break down the functionality into a number of small classes, each of which satisfy a responsibility.
A simplistic way would be for the GUI class to create listeners which listen to the user events. In response the to the use event they fire events that your logic registers itself for. Then when the event is received the logic class can perform the functionality.
You should read about observer pattern, event driven design...
You can read about event driven programming here http://en.wikipedia.org/wiki/Event-driven_programming .
I would instantiate the Logic class outside the GUI, but pass it as an argument to the GUI constructor. It's nearly equivalent to what you are already doing, but I think it makes it clearer that the GUI uses a Logic object. Also, it's possible that Logic does some other things before/after the GUI starts/closes; it might not be the case now, but it could be true in the future if you extend your program.
Many other answers tell you to look at MVC, but that might be overkill for your project. It can decrease complexity for a large project, but increase it for a small one.
EDIT:
Logic login = new Logic();
...
MyGUI gui = new MyGUI(logic);
...
I would suggest you do some researches on the MVC architecture. Your GUI (view) shouldn't interact directly with your model (logic). Implement a controller that will get the "signals" from your view and will be in charge to create your "logic objects" and work with them.
You can create on object of type Logic in your main and store a reference of the object in your Window object - so you can access your Logic object as long as the window exists.
You should look up the Singleton design pattern for such trivial scenarios.
By default, Java uses Reference variables. Hence, if you instantiate your object in GUI class, make sure you send the object via method calls to your processing class.
Alternatively, you can look into singleton classes, which will return only one instance of the class. Inside that class, instantiate all the objects that you will need globally, and re-use that instance throughout your program.
Generally you can. If your application is very simple.
But this approach is not scalable. As your application gets more complex it became much harder for development and support. Try to consider Model–view–controller pattern to define a best way for your design. (according to your nick name I'll take a risk to propose an alternative link)