title: Singleton pattern with parameter
body: <p>in my iPhone application, I'm using a subclass of AFHTTPClient to access a rest web service. I want all my requests to be handled by one instance of my API client so I use a singleton pattern.</p>
  
  <p>This works fine when the service is running only on once URL. I can use a constant value to set the URL.</p>
  
  <p>Now, in the final version of the application, each app will actually talk to another service that will be installed in the corporate network.</p>
  
  <p>So I will be getting the service URL from a remote configuration. Is the singleton pattern still a good choice here? How am I supposed to parameterise it if the URL could actually even change during the runtime of the app ?</p>
  
  <p>cheers</p>
  
  <pre class="lang-c prettyprint-override"><code>#import "FooAPIClient.h"
  #import "AFJSONRequestOperation.h"
  
  static NSString * const kFooAPIBaseURLString = @"http://192.168.0.1";
  
  @implementation FooAPIClient
  
  + (instancetype)sharedClient {
      static FooAPIClient *_sharedClient = nil;
      static dispatch_once_t onceToken;
      dispatch_once(&amp;onceToken, ^{
          _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:kFooAPIBaseURLString]];
      });
  
      return _sharedClient;
  }
  
  - (id)initWithBaseURL:(NSURL *)url {
      self = [super initWithBaseURL:url];
      if (!self) {
          return nil;
      }
  
      [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
      [self setDefaultHeader:@"Accept" value:@"application/json"];
  
      return self;
  }
  
  @end
  </code></pre>