• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use a singleton or a "purely static" class?  RSS feed

 
JohnWilliam Fitz
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.

I have a web application that reads and writes from a text file. The different servlet request threads may need to read or write to/from this file, and so I want the read/writes to happen one at a time (synchronized that is), of course. Performance is not an issue in my particular case. It will only be used by two or three people at a time.

So, at first I thought I create a singleton class with synchronized NON-static read and write methods. The singleton would have a NON-static File member object that it uses for access to the file. A worker thread would call...

MySingleton instance = MySingleton.getInstance();
instance.read();
instance.write();

...and so forth. It seems that should work.

But then I thought, why not just make a purely static class? By that I mean that all the members functions (read() and write()) would be static and synchronized, AND the File member object would ALSO be static. It seems to me that accomplishes the very same thing without the overhead of writing a getInstance() method and making sure that only one instance is ever created of my object.

So, unless I am missing something (and I usually am), in this case functionally there is no difference in the two approaches and the "purely static" approach is less code and (who knows) maybe less memory consumption?

Can anyone tell me if I'm wrong? Should I be using a singleton instead?

Thanks!
John
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you plan on writing any unit tests for this class?
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a web application, the standard way to have only one object is to make it an attribute of the application. Which you get at via the servlet context.
 
Parambir Singh
Ranch Hand
Posts: 40
Android Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a "standard dilemma" - whether to use static methods only class or a singleton. Both approaches are fine from a performance point of view. One advantages of a singleton approach can be that you can extend the class to implement some different functionality of the methods. With static method only class, there's no meaning of inheritance.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a singleton has only private constructors, you cannot extend that class.
 
JohnWilliam Fitz
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Garrett Rowe wrote:Do you plan on writing any unit tests for this class?


Hi Garrett.

Um, possibly. Should that make a difference in my choice?

John
 
JohnWilliam Fitz
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:In a web application, the standard way to have only one object is to make it an attribute of the application. Which you get at via the servlet context.


Hi Paul.

Thanks, I may do that as well. But it seems to me that alone isn't enough. It won't prevent some thread from setting the chosen attribute to null, then instantiating a brand new object (i.e. different instance) and setting the new object as the attribute. Also I need orderly startup and shutdown so that the file is open before the different threads start calling read() and write(), and so the file closed properly when the application is shutting down.

John
 
Parambir Singh
Ranch Hand
Posts: 40
Android Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If a singleton has only private constructors, you cannot extend that class.


Yes but we can have protected constructors.
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JohnWilliam Fitz wrote:Thanks, I may do that as well. But it seems to me that alone isn't enough. It won't prevent some thread from setting the chosen attribute to null, then instantiating a brand new object (i.e. different instance) and setting the new object as the attribute. Also I need orderly startup and shutdown so that the file is open before the different threads start calling read() and write(), and so the file closed properly when the application is shutting down.


I suppose it depends on what kind of programming environment you're in. If you work in an environment where you have to code to prevent other programmers from doing idiotic things, then you have a point. I don't work in that kind of environment so I don't worry about that.

As for your second point, the orderly startup and shutdown can be provided by a ServletContextListener.

Edit: looking at more API documentation, I see there's a ServletContextAttributeListener where you can find out when attributes in the servlet context are replaced. You could write one which puts the original instance back when someone else changes the attribute.

Of course that doesn't prevent people from inserting their own ServletContextListeners and ServletContextAttributeListeners to counteract yours, but you might want to just do code reviews instead of Maginot Line programming.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parambir Singh wrote:Yes but we can have protected constructors.
And how will you ensure that only one instance of the class can be created?
 
JohnWilliam Fitz
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul wrote: but you might want to just do code reviews instead of Maginot Line programming.


Thanks Paul.

I can't find a definintion of Maginot Line Programming. What is that? Is it a bad thing? Are singletons bad?

John
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!