deucalion0
deucalion0

Reputation: 2440

How to use the Singleton pattern via inheritance?

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

Answers (3)

Tigran
Tigran

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

Péter Török
Péter Török

Reputation: 116266

I sense several design smells here.

  • DB connection should not be Singleton - as you yourself mention, connections come and go, while the main point of Singleton is that it stays for the app's lifetime
  • Singleton and thread safety are not a good match
  • Game characters should not have to work with the gateway (c'mon, what is a warrior to do with a database? ;-)

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

Ňuf
Ňuf

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

Related Questions