Featured Post

Smart Phone – The automatic and smart settings app

Smart Phone makes your Smartphone even smarter than it already is. It automatically adjusts the settings of your Android phone based on conditions like location, time, battery and sms text. The main targets of Smart Phone are: automatically change settings to save time manage that in a very efficient...

Read More

Global objects with Singleton

Posted by Bartinger | Posted in All, Tutorials | Posted on 02-08-2011

Tags: , , , ,

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.