1

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.

1

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I understand what you are saying, design patterns are a pain, I have spent A LOT of time researching the singleton as well as other patterns. I will consider your suggestions, thank you!
1

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

1 Comment

Through all the research I have done, I see more negative comments with regards to the Singleton pattern than anything else. I need to implement it in my coursework, that is the only reason I am continuing on with it, otherwise I wouldn't waste my time :(
1

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.

1 Comment

Hi there, could you show me an example of how to do this, it sounds like it may be a solution. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.