Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7829581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:50:08+00:00 2026-06-02T10:50:08+00:00

I am trying to use the Singleton design pattern via my abstract Charcter class

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:50:09+00:00Added an answer on June 2, 2026 at 10:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to unit-test some classes that make use of a Singleton class whose
I am trying to use the singleton method to access a global object (in
I'm trying to create a simple to use singleton class to connect to mysql
I'm trying to implement the singleton design pattern i found in a book, i'm
I'm trying to use a Singleton Class (its a program-wide debug logger called 'PrisLog')
I'd like to use the stackable trait pattern with singleton objects, but i can't
I'm trying to use Google Guice with the @Inject and @Singleton properties as follows:
I'm trying to create a Singleton object that deals with servicing an event listener:
I'm trying to design a set of factory classes for our system, where some
I have a singleton here that I am trying to use the same instance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.