Sunday, April 5, 2009

Parametric Singleton Pattern

We were asked to study Parametric Singleton patterns as an assignment. It was a long time ago, and I just thought today that I can write a post on it... just for the sake of having something new on my blog, if not for anything else. :P

You implement Singleton, when you want that only one instance of your class can be created. A single instance is created the first time, and everytime an instance is requested the same instance is returned. You do this by making the constructor private, and creating a function which takes care of creating an instance when the first request is made and then returning that instance on subsequent requests.

Now what happens when you want to maintain single instances pertaining to particular parameters. You could go with creating multiple Singleton classes, but it would be a complete waste of time if the three classes do the exact same thing but differ only in the parameters.

While searching, the best example I found that explained to me the need of having Parametric Singleton was of Logging.

When you create an application, most of the times you need to maintain a log file, which keeps track of all the things the application is doing. Let's say your application connects with a database and plays with the data and manipulates it depending on certain logic. It iterates through each row and makes a decision on what to do with it. Let's say you wanna keep track of all the rows it iterates through and the decision it takes. And you decide to log it all in a file. For this specific purpose Singleton Pattern will do just fine. In fact it would do just great... But what if your application needs to maintain more than one log. Let's say your application connects with an external interface and it needs to maintain a log for that, then there are things that the application does internally and it needs to maintain a log for that too. So now we have two logs, creating two separate singleton classes for each kind of log is not a very good option because if you get down to it, it's simply writing in two different log files, just the path and the name of the file is different, all the rest remains the same. So why have two different classes which do the exact same work, why not have one Singleton class which could this time return two instances pertaining to each file type (external interface and one for internal working).

So:
  • In a parametric singleton pattern a single instance of a class is created for a parameter.
  • It is based on the Singleton design pattern in which a single class is created. Whenever an object of that class is required the same object is returned.
  • Similarly, in parametric singleton pattern a single object is created pertaining to each parameter.
  • The instances are created and put in a hash table, array list etc. (to keep track of the objects created). When ever an object with particular parameters is requested, this table is checked to determine whether an object containing the specified parameters has already been created or not. If it has, the existing instance is returned; otherwise a new object is created and returned. This new object is saved in the table as well.

The advantages of using Parametric Singleton, I suppose, would include all the advantages you get from Singleton pattern.

You can get rid of creating extra instances of a class now, based on parameters. This couldn't be done with using our traditional Singleton and hence we ended up not-using Singleton and creating multiple instances, though not exactly needed.


Do check out the following sites for better insight:

http://www.c-sharpcorner.com/UploadFile/satisharveti/ParametricSingleton02112009062638AM/ParametricSingleton.aspx (this one's has a good code example)

http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/singlepattern11252008002706AM/singlepattern.aspx
(Singleton Pattern)

http://www.jot.fm/issues/issue_2007_03/column2/

2 comments:

Haris Gulzar said...

A blogger, plus a techie... What a great combination :-). I thought people used blogging to get some time away from their hectic work routines. Yahan tou scene different hai :-)

Intricate said...

Haha..
You can't exactly call me a techie based on a few posts which basically aren't something I invented or discovered.. :P
Anyways, I try to put in the new things I learn on my blog, so that someone might benefit from it (though I don't think anyone ever has :P) and so that I can come back to it for a refresher when I need to. :)