1

It seems to satisfy the three requirements here: On Design Patterns: When to use the Singleton?

I need it to exist only once. I need to access it from all over the source base. It handles concurrent access, i.e. Locks for writes, but can handle concurrent reads.

Hi all,

I've been reading a lot of no doubt intelligent educated and wise gems of advice that Singletons are 'Evil' and singletons are anti patterns or just plain bad news.

With the argument that logging makes sense but not much else.

Just interested to know if the case of essentially a persistent data store context makes sense for a singleton, i.e. Reading/Writing from disk to memory and referencing an object graph.

And if not, how do people normally tackle this issue, I don't currently have any problem with it, I know it's created only once, it's fast and the accessor logic is in one place. Meaning it takes me one line of code to do anything data model related.

Which leaves the only argument that it's bad for testing, in that it's a hard coded production implementation to data, but couldn't I just write a method swizzle through a category or in the test code to create the test version of the singleton?

And the final argument from DI testers, is that it is a hard coded implementation, rather than simply an interface to something, which I do get but I don't really have a major drive to use a DI framework given that I can use protocols for implementation, and use separate init methods for setting up an objects state in testing. There's only ever going to be two types of state for the singleton, or realistically one type...production.

Making it (in my opinion) much easier to read and faster to develop.

Change my view SO?

Community
  • 1
  • 1
David van Dugteren
  • 3,879
  • 9
  • 33
  • 48

1 Answers1

0

Yup, for some singletons are Evil. For the new developers who has little MRC knowledge and more ARC it sounds scary because they need to mess with memory,volatile,synchronize etc.

However it is not against to use them, they indeed has their own purpose to use with some are below.

  • when sharing large data models like arrays and dictionaries etc between multiple screens (VC's) we can't prefer storing them in UserDefaults (because userdefaults is permanent storage and storing such large entries make app start lazily) instead singletons are best since they stay only current app context and restarting app creates new one.

  • when we need a stable db connection to be accessible allover the app without messing up with connecting and closing in every business classes we can go for it.

  • when we wanted an ability to app for theming itself dynamically we would need to create a singleton class which holds all the color,image instances etc. and use that instance in application VC/Views etc so no code duplication and re-processing theme occurs in all places.

You dont have to change your view but tweak it a bit to get some positive intention towards singletons.

Hoping this clears it out, thanks

RamaKrishna Chunduri
  • 1,100
  • 1
  • 11
  • 15