Create Singleton from from class
I was asked to repair a logging utility, that should be based on a singleton. The problem is they don't want to change any code other than the class of the logger. I added an inner class and exposed the previously public methods of the inner class by calling them from an equivalent public method of the outer class.
As I only have 2 years in Java I was wondering if there are any serious ramifications of doing this. It's the things you don't know which always seem to bite you in the behind.
Here is an example of the code. If anyone can see anything obvious please tell me.
publicclass Bitacora{
staticprivate InnerBitacora bitacora =null;
public Bitacora(){
createInstance();
}
privatesynchronizedvoid createInstance(){
if (bitacora ==null){
bitacora =new InnerBitacora();
}
}
publicvoid logError(String idUsr, String component, String method,
String message){
if (bitacora !=null){
bitacora.logError(idUsr, component, method, message);
}else{
System.out.println("Error: null instance of bitacora.");
}
}
> Here is an example of the code. If anyone can see
> anything obvious please tell me.
>
> > public class Bitacora {
>
> static private InnerBitacora bitacora = null;
>
> public Bitacora() {
> createInstance();
> }
>
> private synchronized void createInstance() {
> if (bitacora == null) {
> bitacora = new InnerBitacora();
> }
> }
>
> public void logError(String idUsr, String component,
> , String method,
> String message) {
> if (bitacora != null) {
> bitacora.logError(idUsr, component, method,
> od, message);
> } else {
> System.out.println("Error: null instance of
> of bitacora.");
> }
> }
>
Did you say Singleton? I don't think this one is... you will still manage to create multiple instances of the class.
***Annie***
Someone correct me if I am wrong... but there will be multiple instances of the main class and each will have a single instance of the inner class.LOLSo where does it become singleton?***Annie***
Perhaps I am mistaken, but it seems roughly Singletonish. The reference to the inner class is static, so it will be shared between all instance of the outer object. Just to test it out I did this:
public class OuterClass
{
private static InnerClass inner;
public OuterClass()
{
if (inner == null)
inner = new InnerClass();
}
public void showInstanceStuff()
{
inner.showInstanceStuff();
}
private class InnerClass
{
InnerClass()
{
}
public void showInstanceStuff()
{
System.out.println(this.toString());
}
}
}
//tested with this:
public class ThingerMy
{
public static void main(String[] args)
{
for (int i=0; i < 10; i++)
{
OuterClass oc = new OuterClass();
oc.showInstanceStuff();
}
Thread t = new Thread(new Runnable()
{
public void run()
{
System.out.print("New Thread: ");
OuterClass oc = new OuterClass();
oc.showInstanceStuff();
}
});
t.start();
}
}
//Output is this:
OuterClass$InnerClass@1add2dd
OuterClass$InnerClass@1add2dd
OuterClass$InnerClass@1add2dd
OuterClass$InnerClass@1add2dd
OuterClass$InnerClass@1add2dd
OuterClass$InnerClass@1add2dd
OuterClass$InnerClass@1add2dd
OuterClass$InnerClass@1add2dd
OuterClass$InnerClass@1add2dd
OuterClass$InnerClass@1add2dd
New Thread: OuterClass$InnerClass@1add2dd
The memory location is the same for all, so there is only one inner object being used...
I don't know how well that stands under real scrutiny though.
This is what I was hoping would happen and your code is approximately what I did. I realize that it is not a real singleton but what I needed is that the original class is a singleton, while still allowing the original code to function as is. Does this suit the purpose. I think so and this is going into production in a few weeks on a federal government system (read high load) and I would like to be sure that it will function as I believe it will.
I guess the question is, is the inner class a real singleton and will it behave as such even when access is always through the outer class? Your test which I didn't think of trying, seems to answer this. Anyone else have comments or ideas?
The inner class is a singleton and the outer class is not. If you want only one instance of the outer class, this will not work, and I don't think any other solution can meet the requirements either.