Design Patterns – Using Singleton Pattern for API Manager

design-patternsiosobjective c

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?

Best Answer

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

Related Topic