Blogs

Types of Design Patterns – Singleton Pattern

Moving forward on design pattern as I discussed in previous blog about how we can Solving Design Problems by Design Patterns, so let’s discuss our first pattern Singleton Pattern.

Basically its a design pattern which ensures your system will have only one entry point to access it. It’s sometime very important to have exactly one instance e.g. Our database. In that we always want to have only one access point to maintain the security and similarly in some other scenario also it’s possible. Now as a programmer one can ask, how to ensure that a class has only one instance and prevent all other instances? Lets think a bit on this, A global variable/flag (boolean 0,1)? which will keep track of the instances. No a global variable can never keep track of it as it will not be accessible on other modules of our project. Then what? An experience architect would say that the better solution would be to make the class itself responsible for keeping track of its instance. The class itself only can ensure that no other instance of that is created. At this stage it might look very complex to you but think on it and read it carefully.

For php guys lets walk through the following code.. Syntax will not be different in other languages…

class myClass { public function myClass() { ... } public function myMethod() { ... } public function myNewMethod() { ... } ... }

Now what happens if i want to use this class? simply by creating a new object

$classInstance = new myClass();

But we can create many instances for this class and that is what i want to prevent because i am going to show you how you can restrict multiple instances of this class. So how can we do that?

Like i wrote, the class itself will have to keep track of this and so our class becomes

class myClass {
 
//Variable responsible to keep track.
 
private static $m_pInstance;
 
//constructor
 
public function myClass() { ... }
public function myMethod() { ... }
public function myNewMethod() { ... }
...
}

Now one can ask why this “private static” thing is used here. Well it’s simple, i declared it private because a user has nothing to do with this variable and we don’t want it to be accessible and “static” because i want it to be a class variable so that it retains it’s value to keep track of instance of it’s class. Second thing that i will do is, i will make this constructor private. What?? Yes!! because we don’t want this class to be instantiated from outside of this class. By making it public we are allowing everybody to create instance of it, which we don’t want. Well what’s next? Will this guarantee for only one instance? Not yet. So far we have prohibited outsiders to use it directly not even a single time then now we have to implement the way it will be used only once. So to do that, i will add a very weird method to this class which will ensure that nobody can create any second instance of this class. So our new class becomes-

class myClass {
 
//Variable responsible to keep track.
 
private static $m_pInstance;
 
//constructor
 
private function myClass() { ... }
 
public static function createInstance() {
//self is used for accessing class members
if (!self::$m_pInstance)   {
self::$m_pInstance = new myClass();
}
 
return self::$m_pInstance;
 
}
 
public function myMethod() { ... }
public function myNewMethod() { ... }
...
}

after reading above code intention should be clear that we have added this function to keep track of instances of the class. Let me explain me little bit about it, i have created a public function to make it accessible from anywhere and static because we are gonna access a static member of the class and we want this method to be available for use without creating any instance of the class. Rest is about checking for any other instance and allowing a instance to be created. So far so good but how shall we use it? Well it’s simple-

$classInstance = myClass::createInstance();   $response = $classInstance->myMethod();

This was the implementation of singleton pattern. Post your comments for your queries.

Content Source: http://www.raakeshkumar.in/

Leave Comments

Your email address will not be published.