Comments on: Singleton class with public constructor https://demo.ritambhara.in/singleton-class-with-public-constructor/ Coding / System Design Interviews Thu, 07 Feb 2013 12:37:26 +0000 hourly 1 https://wordpress.org/?v=6.9.4 By: Pawan Gautam https://demo.ritambhara.in/singleton-class-with-public-constructor/#comment-1843 Thu, 07 Feb 2013 12:37:26 +0000 http://www.ritambhara.in/?p=1730#comment-1843 In reply to Kamal Rawat.

Below code should work but i have not tested on multi-threaded Env.
public SingletonWithpublicCons(SingletonEnforcer singletonEnforcer) {
if (singletonEnforcer == null) {
throw new RuntimeException(“Null not allow…”);
}
if (instance != null) {
throw new RuntimeException(“more than one object”);
}
}

]]>
By: Kamal Rawat https://demo.ritambhara.in/singleton-class-with-public-constructor/#comment-1842 Thu, 07 Feb 2013 06:24:51 +0000 http://www.ritambhara.in/?p=1730#comment-1842 In reply to Pawan Gautam.

Yes you are right.. this will not stop user from delegrately creating multiple objects of the class.. the constructor is change to take singletonEnforcer object, just to make sure that object does not get created accidently.. default constructor sometimes get called as a side effect (parameter passing, function returning obejct etc..).. but there is no fool-proff way.. unless you create constructor private..

]]>
By: Pawan Gautam https://demo.ritambhara.in/singleton-class-with-public-constructor/#comment-1841 Wed, 06 Feb 2013 12:44:46 +0000 http://www.ritambhara.in/?p=1730#comment-1841 Hi there,
I did not get any notification that you have replied on my comments. Any way below is the code for the same.
Singleton implementation as per your suggestion
————————————————————————
package com.test;
public class SingletonWithpublicCons {
private static SingletonWithpublicCons instance = null;
public SingletonWithpublicCons(SingletonEnforcer singletonEnforcer) {
if (instance != null) {
throw new RuntimeException(“more than one object”);
}
}
public static SingletonWithpublicCons getInstance() {
if (instance == null)
return instance = new SingletonWithpublicCons(new SingletonEnforcer());
else
return instance;
}
}
class SingletonEnforcer {
}
Client which is broking this approach
——————————————————-
package com.puzzler;
import com.test.SingletonWithpublicCons;
public class SingletonWithpublicConsTest {
public static void main(String[] args) {
System.out.println(new SingletonWithpublicCons(null)); // 😛
System.out.println(new SingletonWithpublicCons(null)); // 😛
System.out.println(SingletonWithpublicCons.getInstance());
}
}

]]>
By: Kamal Rawat https://demo.ritambhara.in/singleton-class-with-public-constructor/#comment-1840 Thu, 24 Jan 2013 14:42:14 +0000 http://www.ritambhara.in/?p=1730#comment-1840 In reply to Pawan Gautam.

Can you paste the code to see where exactly you are getting the problem..

]]>
By: Pawan Gautam https://demo.ritambhara.in/singleton-class-with-public-constructor/#comment-1839 Thu, 24 Jan 2013 13:39:13 +0000 http://www.ritambhara.in/?p=1730#comment-1839 Hi,
I am not flex developer but i use java which allow me to define a constructor private. but the above approach with public constructor is not working in java.

]]>