How does Command Pattern enables Undo? [duplicate] - java

This question already has answers here:
Best design pattern for "undo" feature [duplicate]
(3 answers)
Closed 6 years ago.
Can someone help me understand how can the Command Pattern in Java or C# enables "undo" functionality in application design? Any explanation would be appreciated.

Undo functionality requires that you store the last n-number of user actions. Typically a user action simply results in a function call, so its very hard to store it. Its even harder to store it in a way that is undoable.
By abstracting a user action into an object, you can easily put a set of these objects into a data structure (typically Stack) and if you put an Undo method onto the object you just go through the structure and call the Undo method as the user requests the undo operation.
Since Command is already set up to encapsulate an operation (user or otherwise) it is an excellent starting point for building undo functionality.

Related

Saving permanently user input for later use and expanding your program [duplicate]

This question already has answers here:
Best way to save data in a Java application?
(4 answers)
Closed 2 years ago.
EDITED QUESTION
I made a small sequential program in Java which can calculate my daily bonus for my job. It's not the most beautiful or optimized bit of code but hey it works!
Now it would be cool to have this actually save my input for that day somehow and even when I terminate the program the next time I open it should still have stored that end value somewhere. Maybe even go back to that specific day and check my PPH (productive hours) and maybe even calculate the whole month (which is actually the end goal and track my performance).
Any suggestions?
***** Answers I found ******
This is the first one i found but is not super useful if you would like that your colleagues also use your application. For single use and store a file locally it would work. https://attacomsian.com/blog/gson-read-write-json#
Using an SQL data base: https://www.ntu.edu.sg/home/ehchua/programming/java/JDBC_Basic.html

Restrict the If block [duplicate]

This question already has answers here:
Run a piece of code only once when an application is installed [duplicate]
(4 answers)
Closed 5 years ago.
I have an if condition in my Java code in Android and I want to enter the block it's the first time the user has run the app. But for the second time and thereafter, they ignore the conditions.
How can I restrict the if block in my Java code?
This is my if block condition:
if(AppSharedPrefs.getInstance(SplashActivity.this)
.readPrefs‌​(SplashActivity.this‌​, "language").isEmpty())
{
AppSharedPrefs.getInstance(SplashActivity.this)
.writePrefs(S‌​plashActivity.this, "language","English");
}
You need to have shared preference for your purpose. On first time store value true to shared preference and when next time user comes to the app get value from shared preference if that is true you dont need to call your code
"Simple": you pick one of the options for storing data on the device.
And then, your code reads that data back every time the application starts.
See here for an overview of options, and there for the option you should pick (shared preferences).
In other words: the whole idea is that you have to enable yourself to store data; so that your code can later make decisions based on that data. That is all there is to this.

Trigger email using java after 2 hours [duplicate]

This question already has answers here:
Scheduling a time in the future to send an email in Java or Python
(6 answers)
Closed 5 years ago.
I want to trigger an email with user details in that email after two hours if the user updated basic information. I used TimerTask class in java and it was working fine. But planning to use ScheduledThreadPoolExecutor.java.
My question is,
Since using multithreading, I must avoid race condition (For that I am using ScheduledThreadPoolExecutor.java or TimerTask.java).
User can update his/her basic details N number of times within two hours.So, after two hours I have to send mail with updated details.How to track that timing?
Is there any better approach for this?
Since using multithreading, What are the scenarios I have to cover?
Thanks in advance,
A quick approach is, each time when you do such update email, capture all the necessary data (i.e. copy it) within the task so you totally avoid locking concern. Given your application I think this is sensible and sufficient.
For 2) depends on if you wish to do conflation (i.e. keep only the latest task in the queue) or you wishes to just let me be.

How to implement keystroke logging by java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do you record keystrokes when operating on another window in Java?
Try to get all keystroke on the OS. any ideas?
Yeah possibly u should get the keystrokes of the users through the use of currentTimeMillis method or so in java in any of the keyPressed() or keyReleased() methods in java.Then save those time recordings in any variable get the time difference and then compare it with the keystrokes stored in database with a particular threshold.It should work then.During the recording phase make the user type the intended filed 5-6 times, take the keystrokes recording of the user and then save its mean in the database and compare it with the current keystrokes of the user while he tries to log in.

Java: how to accomplish isKeyDown() functionality that is independent from java components? [duplicate]

This question already has answers here:
React on global hotkey in a Java program on Windows/Linux/Mac?
(6 answers)
Closed 8 years ago.
With Java, I need to detect whether a given key (using it's keyCode) is pressed down or not at a certain moment in time.
You might suggest for me to create an event listener for all keys for a certain component and store all keys' states (pressed down or not). However, I need to know whether a key is pressed down in general, and not for a certain Java component.
For example, if my Java application is running in the background and a user has a key pressed down inside OpenOffice, I need to be able to detect so.
How would I accomplish this isKeyDown() functionality that is independent from java components within Java?
Any feedback is appreciated.
Can't be trivially done in Java. At best you can try JNI

Categories

Resources