Featured Post

I was at Gamescom 11!

Typically I don’t write about topics like this, but I really liked it there and i decided to write a few words and show some photos I had taken. I stood up early in the morning on the 19th of august and flew  with 3 friends and just a backpack to Cologne. Arrived at Gamescom my first impression:...

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.