Featured Post

Get Minecraft 1.8 pre-release

Can’t wait for the next Minecraft update? Mojang released (not the official release) a Minecraft 1.8 pre-release update. Notch is happy for you to try it. Final release at: 12th of September How to get and install it: Download the new minecraft. jar  http://assets.minecraft.net/1_8-pre/minecraft.jar Go...

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.