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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:54:01+00:00 2026-05-11T17:54:01+00:00

I’m looking for a design pattern to manage a pool of objects. A thread

  • 0

I’m looking for a design pattern to manage a pool of objects. A thread can request an object, in my case an object representing a remote host.

If the host-object does not exist it is created, added to the pool and a reference returned.

If another thread or object requests the same (remote host) object it too is given the reference. If a different remote host is requested it is created and added to the ‘pool’ of objects.

Once the host-object is no longer needed by any thread or object is removes it self form the pool.

  1. Is there pattern for this, and what is it name?
  2. I will be implementing this in Objective-C, any specific help would be useful.
  • 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-11T17:54:01+00:00Added an answer on May 11, 2026 at 5:54 pm

    An alternative to using NSMapTable (which is unavailable on the iPhone, for instance) is to use the CoreFoundation equivalents. Those can contain regular non-retained pointers. You still need to pull things out of that list however, but that can be done easily in an object’s -dealloc method.

    One possibility is using a CFSet. This would require that your objects all respond to -isEqual: and -hash properly, so that the set can determine if an object is already present accurately.

    For instance (note that I’ve not compiled this but it ought to be at least 90% correct):

    static id __uniqueObjects = nil;    // GC uses NSHashTable, non-GC uses NSSet (inited using CFSet APIs)
    
    @interface MyHost : NSObject
    {
        NSURL * _hostURL;
    }
    
    @property (nonatomic, readonly) NSURL * hostURL;
    
    + (MyHost *) uniqueHost: (MyHost *) aHost;
    + (void) removeUniqueHost: (MyHost *) aHost;     // only used in non-GC environment
    
    - (id) initWithURL: (NSURL *) hostURL;
    
    @end
    
    @implementation MyHost
    
    - (id) initWithURL: (NSURL *) hostURL
    {
        self = [super init];
        if ( self == nil )
            return ( nil );
    
        // use copy, because NSURL implements NSCopying protocol
        _hostURL = [hostURL copy];
    
        // if there's a version around already, release this instance
        MyHost * result = [[self class] uniqueHost: self];
        if ( result != self )
        {
            // set _hostURL to nil so we don't inadvertently remove anything from uniqueing table in -dealloc
            [_hostURL release];
            _hostURL = nil;
            [self release];
        }
    
        // return either self or a pre-existing unique instance
        return ( result );
    }
    
    - (void) dealloc
    {
        // non-GC objects need to explicitly remove themselves from the uniqueing table
        [[self class] removeUniqueHost: self];
        [_hostURL release];
        [super dealloc];
    }
    
    // no need for -finalize -- under GC we use NSHashTable with weak references
    
    - (NSUInteger) hash
    {
        return ( [_hostURL hash] );
    }
    
    - (BOOL) isEqual: (MyHost *) other
    {
        if ( other == self )
            return ( YES );
    
        return ( [_hostURL isEqual: other->_hostURL] );
    }
    
    + (MyHost *) uniqueHost: (MyHost *) aHost
    {
        if ( __uniqueObjects == nil )
        {
            // use low-level routine to check, because iPhone has no NSGarbageCollector class
            // we use NSHashTable or NSMutableSet, because they both respond to the same messages (NSHashTable is modeled on NSMutableSet)
            if ( objc_collectingEnabled() )
            {
                // hash table has zeroing weak memory, object equality (uses -isEqual:), and does NOT copy objects, just retains them
                __uniqueObjects = [[NSHashTable hashTableWithWeakObjects] retain];
            }
            else
            {
                CFSetCallBacks cb = {
                    0,                  // version
                    NULL,               // retain (non-retaining references)
                    NULL,               // release (non-retaining references)
                    CFCopyDescription,  // CF (plain C function) equivalent for -description
                    CFEqual,            // CF (plain C function) equivalent for -isEqual:
                    CFHash              // CF (plain C function) equivalent for -hash
                }
                __uniqueObjects = (NSMutableSet *) CFSetCreateMutable( kCFAllocatorDefault, 0, &cb );
            }
        }
    
        MyHost * result = nil;
        @synchronized(__uniqueObjects)
        {
            // treat both like an NSMutableSet & we're golden
            // we use the -member: function to get an existing matching object from the collection
            result = [[__uniqueObjects member: aHost] retain];
            if ( result == nil )
            {
                // store & return the passed-in host object
                [__uniqueObjects addObject: aHost];
                result = aHost;
            }
        }
    
        return ( result );
    }
    
    + (void) removeUniqueHost: (MyHost *) aHost
    {
        if ( __uniqueObjects == nil )
            return;     // shouldn't ever happen, but it's a good idea to check at least
    
        @synchronized(__uniqueObjects)
        {
            [__uniqueObjects removeObject: aHost];
        }
    }
    
    @end
    

    Hopefully I’ve put enough in here to answer any questions/thoughts you might have about this design pattern.

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

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can do that. The problem is the inheritance modifier… May 11, 2026 at 11:32 pm
  • Editorial Team
    Editorial Team added an answer Assuming your controller class derives from System.Web.Mvc.Controller (probably this is… May 11, 2026 at 11:32 pm
  • Editorial Team
    Editorial Team added an answer It's because the this scope refers to a cfc instance.… May 11, 2026 at 11:32 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.