Design patterns: Singleton
Since I’m now finally graduated from university, I am supposed to know everything from writing code at CPU level to managing a full blown information system project, but I think I’ll start off with something a little lighter; design patterns.
Design patters are tested and proven solutions to specific problems. It’s sort of a blueprint to solve the problem for you, which means you don’t have to reinvent a solution for the same problem over and over. The idea of using patterns in software development was first coined by” the Gang of Four” (Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides) in their book “Design Patterns: Elements of Reusable Object-Oriented Software”. This book is considered a bible to many design pattern evangelists, don’t let that deter you from reading it though. It is an excellent book on design patterns and object oriented development.
Singleton is one of the patterns that were included in “the Gang of Four” book. It is a pattern that is probably the easiest one to understand, but also one that is very easy to misuse. And because of that it is not very popular in some circles. Singleton is a way of making sure there is only one instance of the given class, no matter how many times it’s called. This way you can use it to share resources, like a database connection, between different objects.
public class A
{
Singleton single = Singleton.getInstance();
single.number = 12;
}
public class B
{
Singleton single = Singleton.getInstance();
Console.Print(single.number); // prints 12
}
Even though both classes in the above generates an instance of the Singleton class, they get the same instance, this way you don’t have to pass the singleton instance between classes to use it. Though, they will still have access to the same resources, so you can e.g. limit the number of persistent connections to the database with one.
There are some disadvantages with using the singleton, the biggest being that once implemented; it will take a lot of work to change away from it. Let us say you have to add a second database to your web application. If your database manager is a singleton, it can become very hard. A second problem is that it can hide implementation details, which can make maintenance and bug finding harder; it is often referred to as a “super global” because of this.
There are a couple of key points to making a singleton. You will have to disable the creation of new instances from outside of the class itself. If you are allowed to create new instances with the ‘new’ keyword, you can end up with several instances of the singleton class. Also, any clone methods have to be disabled.
Implementation in php
class Example
{
// Hold an instance of the class
private static $instance;
// A private constructor; prevents direct creation of object
private function __construct()
{
echo 'I am constructed';
}
// The singleton method
public static function singleton()
{
if (!isset(self::$instance))
{
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
// Example method
public function bark()
{
echo 'Woof!';
}
// Prevent users to clone the instance
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
}
Implementation in C#
public sealed class Singleton
{
private static readonly Singleton instance=new Singleton();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Singleton()
{
}
private Singleton()
{
}
public static Singleton Instance
{
get
{
return instance;
}
}
}
Implementation in Java
public class ClassicSingleton {
private static ClassicSingleton instance = null;
protected ClassicSingleton() {
// Exists only to defeat instantiation.
}
public static ClassicSingleton getInstance() {
if(instance == null) {
instance = new ClassicSingleton();
}
return instance;
}
}
2 Responses
whoah this blog is wonderful i love reading your posts. Keep up the good work! You know, many people are searching around for this info, you can help them greatly.
I have observed that some viewers ultimately “acquired it” when they watched it with a DVD player so they could freeze selected scenes with the pause button. Particular vital hallucinations or catatonic are challenging to adhere to on an preliminary viewing. Making use of pause will obvious every little thing up, but it appears like Pitt ends up enjoying mysterious characters really often. If you like action, murder, mayhem, and emotional thrillers you will appreciate Battle Club.
April 15th, 2012 at 05:11