PrajwalAS
PrajwalAS

Reputation: 41

iOS Singleton class inside MVC architecture

Most of the iOS apps are based on MVC Design pattern, and we are using Singleton classes inside our apps from many years. I know that Singleton itself is a design pattern and many consider it as anti-pattern and all that good bad stuff about singletons.

But if you consider MVC as a overall architecture to your app development I would like to understand where does a singleton be categorised in it. Is it a Model, or View or Controller? if so why? if it does not belong to any of these three then what it is?

This might seem a silly question but it keeps bothering me as I was asked this question in an interview in a reputed company.

Upvotes: 1

Views: 325

Answers (1)

Toby
Toby

Reputation: 1548

MVC is an architectural pattern, whereas Singleton is a design pattern. The two things are not related, and therefore Singleton is not 'anything' in MVC.

Leaving aside any political discussion about the use of Singletons, you could in theory decide to make any Model, View or Controller class a Singleton.

As gnasher has pointed out, within the iOS SDK, ViewControllers are created and destroyed by the runtime, which means it would be illogical for them to be Singletons in that context, regardless of theory.

A more likely scenario is that you want your Model to be available to multiple Views. You might decide to make that Model a Singleton which can be accessed through a dependency injection framework, or constructor injection. In that case, your class is both a Model class, AND a Singleton. They are not mutually exclusive.

I wonder if that is what your interviewers meant - where would you most likely want to use a Singleton in an iOS app? In that case, the correct answer would be in the Model.

Upvotes: 3

Related Questions