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 6815263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:47:11+00:00 2026-05-26T20:47:11+00:00

In my class Deck i have static Deck *gInstance = NULL; +(Deck *) instance

  • 0

In my class Deck i have

static Deck *gInstance = NULL;


+(Deck *) instance {
    @synchronized(self) {
        if (gInstance == NULL)
            gInstance = [[self alloc] init];
    }

    return (gInstance);
}

and an init method that looks like

-(id) init {

    if (gInstance != NULL) {
        return self;
    }

    self = [super init];

    if (self) {
       // Lots of clever things
    }
    gInstance = self;
    return self;

}

My concern here is mainly whether init is implemented correctly. Please let me know if what i wrote looks right to you.

Or … is there a way i can make init private and prevent people (myself included) from seeing it altogether?

  • 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-05-26T20:47:11+00:00Added an answer on May 26, 2026 at 8:47 pm

    That is a kind of odd singleton implementation. My favorite implementation is to use some newer functionality present in GCD.

    + (MyObj*)sharedObject;
    {
        static dispatch_once_t once;
        static MyObj *sharedObj;
        dispatch_once(&once, ^ { shared = [[MyObj alloc] init]; });
        return shared;
    }
    

    I would recommend only doing that, nothing more, nothing less. Enforcing a strict singleton will only end in pain and is generally pointless and an anti-pattern.

    As far as if there is anything strictly ‘wrong’ with your implementation, I believe you’d want to return gInstance in the initializer when it isn’t NULL, not self.

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

Sidebar

Related Questions

I have a class, Deck , that contains a method called Shuffle . I'm
Imagine that I have a nice Deck class, in the best OO fashion. It
I have a class Model: class Model { ... boost::shared_ptr<Deck> _deck; boost::shared_ptr<CardStack> _stack[22]; };
I have a method to generate a Deck object (NSObject subclass with an NSMutableArray
I have a Card class subclassed from UIView and a Deck class from NSObject.
I've come across a really strange problem. I have written a simple Deck class
I have the following model: class Deck < ActiveRecord::Base has_many :cards serialize :stats attr_accessible
I've written a Deck class and now have to shuffle it, and then print
I have a class CardStack . I have several classes that inherit from CardStack
So I am writing a class that plays the card game war. I have

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.