Global objects with Singleton
Posted by Bartinger | Posted in All, Tutorials | Posted on 02-08-2011
Tags: android, java, pattern, programming language, software
4
Singleton is a software pattern which ensures that there is only one object of an class. Furthermore this object is globally available. I use this pattern in many of my projects for logging, because it’s really efficient, saves memory and easy to implement. You use singleton if
- there has to be only one object of a class and need a simple access to that object or
- the single object is specialized by subclassing.
A few practical examples:
- for logging (maybe into a file)
- loading data (i.e. sqlite)
I will show it using Java and then give you some worth knowing tips for Android, but the singleton pattern works with every object-oriented programming language.
