Solved

Java singleton - elaborate

Posted on 2014-11-30
2
182 Views
Last Modified: 2015-02-23
I wonder when it is a bad idea to use singletons in Java.

Specifically I am to build an API where files will be submitted to my API (or actually the file name and then the file will be located in a specific directory).

There are no specifications on how many transactions needs to be handled. If there would have been my stomach tells me it would be good to have a singleton taking care adding to of the inqueue and making sure it is not bigger then MAX_ALLOWED_SIZE (a value set to a reasonable figure to make sure the system is stable).

Something I do know is that I need to have a very good idea on how to make the code testable. Something that is abit tricky when it comes to singletons.

-JUnit
-On the system level - performance wise maybe

Please elaborate on when to use a singleton and when not, and also how to handle Junit testing and also if possible a good way to handle performance testing.
0
Comment
Question by:mdoland
2 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 250 total points
ID: 40472891
I wonder when it is a bad idea to use singletons in Java.
It is a bad idea to use singletons when singletons don't fit with your design. And yes, that statement was intentionally vague because your question is so open, ie. it totally depends on each individual situation as to whether a singleton is a good/bad idea. Also you can take from the above, that there is nothing "inherently" bad about them, they just should be used when the right situation calls for it. Note also that the above is talking from a framework agnostic point of view, and if you are referring to specific frameworks then the above situation might change somewhat. For example, if you are using Spring then singletons are using quite extensively, whereas others (I think J2EE might be one, I've never really used it though) don't really use them at all.

As for issues regarding testability, then whether you have singletons or not should make NO difference what so ever. How you design your classes and how you implement that design will be the telling factor on how testable you code is. Ensuring that you keep a good separation of concerns and clean API's between your classes is a first step to ensuring testable code.
0
 
LVL 26

Assisted Solution

by:dpearson
dpearson earned 250 total points
ID: 40473094
The concerns people have with Singletons for testing is that they may want to replace the object managed by the Singleton with a mock object.  This may or may not be easy.

For example:

public class Database {
    private final static Database instance= new Database("jdbc:mysql://...") ;

    public static Database getDatabase() { return instance ; }
}

Since this Singleton is tied to a real database which it initializes itself, it may make unit testing of other elements harder.  For instance it's common to replace calls to an actual SQL database with a mock database during testing - perhaps an in memory database.  The above class makes that hard to do.

The common fix is to have the Singleton not initialize itself, but be passed in:

public class Database {
    private final static Database instance = null ;

    public static void initialize(Database inst) {
         if (instance != null)
            throw new IllegalStateException("This is a singleton - only allowed to set it one time") ;

         instance = inst ;
    }

    public static Database getDatabase() { return instance ; }
}

This makes the Singleton more testable, but makes initialization more complex in return (somebody has to remember to initialize this before it's used and that has to happen exactly once).

As mccarl says, it depends on the specifics of the problem whether there's an issue with what you're trying to do with a Singleton or not.  But that's the general concern.

Doug
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java array passed to SQL where clause 23 63
servlet  URL Rewriting 1 27
replace a word with other 1 40
windows explorer path to command prompt 5 33
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now