Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can implement single with public constructor? and if yes what is the advantage of doing this?
Posted
Comments
PIEBALDconsult 8-Jan-15 8:14am    
Sure, just instantiate only one of it. But seriously, don't use Singletons, it's an anti-pattern.
Sudhir Dutt Rawat 8-Jan-15 8:17am    
You mean to say write instantiate logic in constructor?
OriginalGriff 8-Jan-15 8:22am    
"don't use Singletons, it's an anti-pattern"

Now, now! :laugh: The Singleton Pattern is a highly respected way of doing things, and I'm sure that one day I'll find an example that can't be better done a different way and then I'll know why it is respected!
It's taught by lazy tutors because it's simple to explain but sounds fancy. So I don't think he will implement it more than once - much like most Patterns... :sigh:
PIEBALDconsult 8-Jan-15 9:18am    
"highly respected"

You mean "respected by people who must be high"?
BillWoodruff 8-Jan-15 10:06am    
Put money where mouth is and please to use your (amply having given proof of excellence of) intellect to assist OP (and everybody) by explaining your latest fatwa banning Singleton.

Well, you can...but then it isn't a singleton class.

The idea of a singleton class is that it only allows a single instance of the class to exist - so making the constructor public circumvents this by allowing the world outside the class to call the constructor and create as many instances as it wanted: unless the constructor looked for this and threw a run-time error. Which is poor design, because it creates runtime problems that should have been caught as compile time!
 
Share this answer
 
Comments
Sudhir Dutt Rawat 8-Jan-15 9:55am    
I agree .. I had same argument but other guy said it is possible and have one advantage just try to figure it out...
Dave Kreskowiak 8-Jan-15 11:31am    
Well, if he didn't provide an explanation of the "advantage" and a working code sample, it is only his opinion and not a valid argument.
Sudhir Dutt Rawat 8-Jan-15 13:24pm    
right
Well, yes, you can do something like this:
C#
// non-static Ctor: static internal instance
public class OpenDoorSingleton1
{
    private static OpenDoorSingleton1 _instance;

    private string _aString;

    public string AString
    {
        set { if (_instance != null) _instance._aString = value; }

        get
        {
            if (_instance != null)
            {
                return _instance._aString;
            }
            else
            {
                return string.Empty;
            }
        }
    }

    // hidden parameterless ctor
    protected OpenDoorSingleton1()
    {
    }

    public static OpenDoorSingleton1 StaticInstantiate()
    {
        if (_instance == null) _instance = new OpenDoorSingleton1();

        return _instance;
    }

    public OpenDoorSingleton1(string astring)
    {
        _instance = StaticInstantiate();
        _instance.AString = astring;
    }
}
This has a public constructor that lets you use parameters, but, look at all the trouble you had to go through to avoid recursion and stack overflow here !

I would never use code like this: you are hiding the intention to make this a one-and-only-one instance Class !

See: Jon Skeet: [^], and, the DoFactory sample for Singleton (free): [^].
 
Share this answer
 
Comments
Sudhir Dutt Rawat 8-Jan-15 13:28pm    
Thanks for posting code. I tried similar thing.. but using such code doesn't make scene..as u also said above.

I think singleton should be used as is.
Afzaal Ahmad Zeeshan 8-Jan-15 13:46pm    
+5, but Bill what if there is some condition inside the class itself, set as static. That would determine whether the class has an instance created already or not. Would that voilate the idea of Singleton too? To still have a public constructor, but let only one instance and ignore others using that property?
BillWoodruff 8-Jan-15 14:10pm    
Hi Afzaal, I'm not sure about what would happen in the case you mention ...

I wouldn't be surprised that it would be easy to screw this type of code up ! It doesn't implement thread-safety, for one thing.

To my mind this is "pathological code," of interest for educational purposes only, and should not be used in "real world code" :) However, I think the OP's dilemma with teacher/manager is interesting, and worth a reply.

If I want a real Singleton, I'll follow in the footsteps where Jon Skeet walks, thanks.

cheers, Bill

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900