Reputation: 2440
I am trying to use the Singleton design pattern via my abstract Charcter class so all sub classes can acces the object instance. Here is my singleton class:
class GatewayAccess
{
private static GatewayAccess ph;
// Constructor is 'protected'
protected GatewayAccess()
{
}
public static GatewayAccess Instance()
{
// Uses lazy initialization.
// Note: this is not thread safe.
if (ph == null)
{
ph = new GatewayAccess();
Console.WriteLine("This is the instance");
}
return ph;
}
}
I can use this in my program.cs to create an instance no problem:
static void Main(string[] args)
{
GameEngine multiplayer = new GameEngine(5);
Character Thor = new Warrior();
Thor.Name = "Raymond";
Thor.Display();
Thor.PerformFight();
Thor.PerformFight();
multiplayer.Attach(Thor);
GatewayAccess s1 = GatewayAccess.Instance();
GatewayAccess s2 = GatewayAccess.Instance();
if (s1 == s2)
{
Console.WriteLine("They are the same");
}
Console.WriteLine(Thor.getGamestate());
Console.ReadLine();
}
So what I want to do is allow the subclasses ie, warrior to access the instance of the Gateway, I just cannot figure out how to do this as the inheritance stuff is confusing me. Basically the gateway access is an access point to a database that can only have one connection at once. The singleton pattern was easy enough to understand, its just the mix of that and the inheritance. I was hoping once I achieved this, I could then do it in a thread safe manner.
I was also wondering how the Singleton instance could be dropped, as it is a connection to a database and can only be used by one character object at a time, then once the character object is done with it, it must free the singleton object up right?
I tried to use methods in my Character class to do all this but it isn't working.
I appreciate any help with this.
Upvotes: 1
Views: 614
Reputation: 62246
You can use a simple static class
, instead of singletone, you can not extend it and can not create an instance of it. Plus, you can use it in way you want, by simply invoking a static
unctions on it, and it internally can keep track of the state of private static connection
member.
EDIT
Just a pseudocode example:
public static class Connector
{
static SqlConnection con = new SqlConnection(...); //return type object,
//just for example, choose more
//appropriate type for you.
public static object GetData(string query)
{
con.Open();
//run query and retrieve results
con.Close();
}
}
Hope this helps.
Upvotes: 1
Reputation: 116266
I sense several design smells here.
You should better separate concerns and have DB / persistence handled by a different class which calls the game characters rather than vice versa.
It is difficult to give more specific advice with the little information you provided.
Upvotes: 4
Reputation: 6207
Singleton is definitely not good pattern, when it is supposed to be used by one object only. Why don't you create it as non-static field of that Character class and destroy it in IDispose.Dispose()? If you still want singleton, make 'ph' protected and then you can access it as GatewayAccess.ph
Upvotes: 1