Reputation: 2206
private static PostalCodeInfoTable postalCodeInfoTable = new PostalCodeInfoTable();
public static PostalCodeInfoTable GetInstance() { return postalCodeInfoTable; }
public PostalCodeInfoTable()
{
db = new GetConnectionString());
}
I use the GetInstance() to create a PostalCodeInfoTable, then I use a member function to load some data that I haven't shown here, then I use GetInstance again and then data is already pre-loaded, so it seems we have a singleton, but I'm confused theoretically why we can do this with a public constructor
Upvotes: 0
Views: 3014
Reputation: 59
You could use an enum like so:
public enum PostalCodeInfoTable
{
Instance
}
You can then access this instance using:
var postalCodeInfoTable = PostalCodeInfoTable.Instance;
The Instance value within the enum implicitly represents the single instance of PostalCodeInfoTable. The C# runtime guarantees that enum values are initialized only once, making this a thread-safe way to implement a singleton.
EDIT: Can whoever has marked me down please tell me what is wrong with my solution? An enum is a class, and this is a valid way of creating a singleton without an explicit private constructor.
Upvotes: -1
Reputation: 3025
A singleton cannot be made without a private constructor!
If a class has a public constructor, then anybody can create an instance of it at any time, that would make it not a singleton.
In order for it to be a singleton there can only be one instance.
From wikipedia
An implementation of the singleton pattern must:
- ensure that only one instance of the singleton class ever exists;
- and provide global access to that instance.
Typically, this is done by:
- declaring all constructors of the class to be private;
- and providing a static method that returns a reference to the instance.
Upvotes: 7
Reputation: 9804
What you created here is a static variable (PostalCodeInfoTable.postalCodeInfoTable) that can only be accessed through a static method (PostalCodeInfoTable.GetInstance()).
The PostalCodeInfoTable instance is created by the TypeConstructor, wich is rarely used outside of runtime constant declaration.
The public constructor? That one is totally irrelevant. I am not sure what purpose it has. It can not affect what is int the static field. Indeed all it does is break the single pattern:
var temp1 = PostalCodeInfoTable.GetInstance();
var temp2 = new PostalCodeInfoTable();
//This will return false, as they are not the same instances
object.ReferenceEquals(temp1, temp2);
Upvotes: 1
Reputation: 2161
An implementation of the singleton pattern must:
Typically, this is done by:
The instance is usually stored as a private static variable; the instance is created when the variable is initialized, at some point before the static method is first called.
Correct example:
public class PostalCodeInfoTable{
//.........
private static PostalCodeInfoTable Instance = new PostalCodeInfoTable();
private PostalCodeInfoTable() {
db = new GetConnectionString();
}
public static PostalCodeInfoTable GetInstance() {
return Instance;
}
}
Upvotes: 0
Reputation: 837
A singleton is nothing more that an instance of a class that is expected to have only a single instance. Usually you would hide the constructor by making it private and using a static member to access it, making sure only a single instance can exist within your application.
As the constructor is public, you can still create a new instance. Making it no longer a 'Singleton' by definition, however this does not mean it cannot behave as one.
Upvotes: 2