• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Another Design Question

 
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Day All

I'd like to pick your brains a bit.

I'm working on a simple application. Among other things, it keeps a counter. Whenever the user clicks on a particular button, the counter should be incremented. If the application is closed and reopened, the counter should resume where it was before the app was closed.

What do you think would be the best way to implement this? I can see two options; 1.) Use a database with a single table to keep track of the counter; 2.) Write the counter to a file whenever it is changed. Option 1 seems like overkill, since the database will have a single table with a single column. Option 2 seems like it could lead to a lot of file IO.

Which approach would you go for?

Cheers,
Riaan
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not read the file when the applications starts up and write the new value when the application shuts down?
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This seems to be a perfect use for the preferences API. See http://download.oracle.com/javase/1.4.2/docs/guide/lang/preferences.html and http://java.sun.com/developer/technicalArticles/releases/preferences/ .
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems that Preferrences or Properties are both overkill unless there are
additional persistence needs. You might consider just reading a file on start-up
and writing to it each time the value changes. Be sure to close the file after
each read/write. Job done, and completely crash proof.

Jim ... ...
 
Ranch Hand
Posts: 40
Eclipse IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The counter would count every user-click. To achieve this, we should keep a variable in application scope and its value should be persisted to table/file only when application closes. And when next time the application starts, we can read its value.
Rather than, always reading/writing a value from a table/file at every user-click, which would involve huge amount of reading/writing to table/file.

If we follow this approach, we can use any of the option (file or table) to store the counter value.
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your responses everyone.

I briefly considered writing the file only when the app shuts down, but as Jim pointed out, it's not exactly crash-proof. If there is a power failure or if the app unexpectedly quits for some other reason, the counter won't be written to the file and I'll end up losing a bunch of numbers.

It looks like a file is the way to go - writing to a file a every minute or so probably isn't the end of the world.

@James
Thanks for the link!
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Hoglund wrote:It seems that Preferrences or Properties are both overkil



Sorry but I strongly disagree with this. The OP's requirement is pretty much exactly what the Preferences API is designed for. i.e. persisting a small number of values between program calls. It takes very little code to implement this approach and the detail of where and how the value(s) are stored is abstracted away from the developer. All your concern about opening and closing files is handled by the Preferences API so becomes a non-problem.
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Riaan Nel wrote:
@James
Thanks for the link!



Why did you reject the Preferences API? It is exactly what you need. Just about anything else is re-inventing the Preferences API. Please please please do yourself a favour and re-consider.

 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Sabre wrote:

Riaan Nel wrote:
@James
Thanks for the link!



Why did you reject the Preferences API? It is exactly what you need. Just about anything else is re-inventing the Preferences API. Please please please do yourself a favour and re-consider.



I agree with James. Preferences API gives you the power to do all kind of fancy things real easy, such as storing counters for each user and different counters for different classes really easy.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, the Preferences API is a good solution, but the problem as stated asks for much less.
Form-follows-function, in this case complexity-follows-need isn't a bad rule to follow, so I would
still do a simple file approach. It doesn't seem that "do[ing] a lot of fancy things real easy" is
part of the requirement. As to doing a lot of open/close work, it's pretty basic stuff - just a few
lines. Plus there's no studying required to make sure that Preferences is used correctly.

Jim ... ...
 
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Hoglund wrote:Form-follows-function, in this case complexity-follows-need isn't a bad rule to follow, so I would
still do a simple file approach.



I didn't see anything in the original requirements about a file. So there's just a need to persist a few values. And since using the Persistence API is simpler than using an approach where you use a file, and you're suggesting to use the simplest possible approach (which which I agree), it follows that the Persistence API is the way to go.

It seems that since you aren't familiar with the Persistence API, you are just trying to rationalize your preference for not using it.
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Hoglund wrote:Okay, the Preferences API is a good solution, but the problem as stated asks for much less.



How much less can one get ?!!!

To get the counter value :-


To increment the counter value :-


If as I do one wraps this in a class then one only has to create the Preferences instance in the constructor and save it as an instance variable and one has the counter value in an instance variable.

I use this basic approach for all my desktop applications for persisting ALL user preferences. I use an event driven approach so each time a value changes anything registered gets notified.
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@James
You've prompted me to take another look at the Preferences API. In all honesty, I was just a bit lazy - at first glance, the Preferences API looked like a lot of stuff to read through in order to perform a trivial class. You've convinced me that it's a better approach, and that it could prove useful in the future. Thanks for your advice and the example code!
 
reply
    Bookmark Topic Watch Topic
  • New Topic