So basically I am writing a wrapper for a REST API in objective-c so that our customer can easily use them in their iOS development.

I am trying to find a good design pattern for this purpose, it seems Singleton Design Pattern is the only choice here. Without a public initializer, it is a shared manager accessible by calling a function and thus every controller in their app can access it.

I am seeking some advice for such a purpose: to design a wrapper for API. Should I use the Singleton design pattern? Or I should allow them to inherit this class and create it on their own risk?

  • 9
    You've failed to explain the requirement here. This should only ever be constructed once.... why? Commented Jul 28, 2018 at 2:37
  • you may refer to this on why we should avoid singeltons: stackoverflow.com/questions/137975/… Commented Jul 28, 2018 at 9:52
  • But note that arguments against singletons are very often based on assumptions about implementation which don't apply to Objective-C. There are only two real arguments against singletons in Objective-C: If they create a bottleneck (possible on an immensely powerful server) - or if you need more than one.
    – gnasher729
    Commented Jul 28, 2018 at 23:44
  • BTW. It is customary in Objective-C to not waste your time on trying to prevent developers from creating a second instance beyond forbidding to use the "init" method outside the implementation of the Singleton class. If they want to shoot themselves in the foot that's not your problem.
    – gnasher729
    Commented Jul 28, 2018 at 23:48

2 Answers 2


Generally the advice is not to use the singleton pattern to implement global variables.

However, here your objective seems to be 'Implement a global variable' as this will 'make it easy' for your customer.

You could well be right, global variables do make it easy to get up and running with something. If your customer needs help writing a client for your api maybe that is exactly the kind of help they want and need.

However, I would do both, allow the client to be instantiated if required AND provide a singleton wrapper class for newbies.

This will cover you for other more advanced customers who want to do things the singleton prevents, whilst also giving you the easy 'just make it work' minimal code tutorial for your 'my first app' customers

  • You Ain't Going to Need It.
    – gnasher729
    Commented Jul 28, 2018 at 23:45
  • And I couldn't find anything about accessing "global variables" in the question.
    – gnasher729
    Commented Jul 28, 2018 at 23:49
  • 3
    A singleton is effectively a global variable - the static instance variable is accessible globally via the static instance function.
    – bdsl
    Commented Jul 29, 2018 at 11:44
  • Bdsl - that’s nonsense. A global variable is accessible without any function. And “static instance function “ is a contradiction in itself. There’s no such thing.
    – gnasher729
    Commented Jul 30, 2018 at 8:05
  • 3
    Really, the correct answer is "Use dependency injection" followed by "Learn dependency injection." However I've worked in code bases that use the singleton pattern for this sort of thing. It is quick to get up and running. When you start encountering the problems you get with singletons, then the other part of your answer is spot on. Give them both. Commented Jul 30, 2018 at 20:34

I recommend against this approach. I suggest that instead of doing that, you make an extension on URLRequest that contains a class method for each kind of request that can be made to your API. These methods should return a properly built URLRequest.

When the user of the library wants to make a request, (s)he can simply construct the URLRequest and pass it to URLSession, AFNetworking or any other networking library they choose to use/create.

It's much more flexible, composable and testable.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.