Understanding Singleton

I remember the days when I used to write programs in frames- moving from one frame to another and then access the _global variables or variables under _root to know the configuration of the state. To day when I start writing a piece of code I think of a good way to implement the same sort of globalization of the variables so that they can be accessible through out my project with the latest modified values. For a quite long period I was not happy with the design patterns thinking that what the heck they do when I can access the class variables by reference. Some times I keep using static variables also to keep my state- it all worked for me but as my programs become large for some of the projects I found if boresome to use references. It also created some misunderstanding in my team members. This resulted in using a very basic pattern known as Singleton.

The need of singleton is very simple – to tell every son that their mothers love is same for all of them. Where every you go you get the same instance of the class through out the application life time- even if you declare two instances you get the refference to the same class. You can identify this type of implementation when you try to create an object of the class. It gives error when you create object- because object of a class takes away a copied functionality of the class which is not here in the case of the singleton. As the class constructors are not private in AS3 you can not have proper singleton implementation like in java. There are two most found implementation of the singleton are

1. By the help of a singleton enforcer

  1. package{
  2.        public class Singleton{
  3.             public static var instance:Singleton;
  4.             public static function getInstance():Singleton{
  5.                  if( instance == null ) instance = new Singleton( new SingletonEnforcer() );
  6.                  return instance;
  7.             }
  8.             public function Singleton( enf:SingletonEnforcer ) { }
  9.             }
  10. }
  11. internal class SingletonEnforcer{}

2. Simple skeletal singleton

  1. package { 
  2.       public final class Singleton { 
  3.        private static var instance:Singleton = new Singleton()
  4.           public function Singleton() { 
  5.               if( instance ) 
  6.                    throw new Error( "Singleton and can only be accessed through singleton.getInstance()" );
  7.          }
  8.           public static function getInstance():Singleton {
  9.                return instance; 
  10.            } 
  11.      }
  12. }

One should not be confused with the use of static variables with the singleton. Singleton has more features than static variables. You can extend singleton classes but with classes with static data member you can not make inheritance. Performance wise singleton is better than a class having all static variables because singleton comes in to play when you make the first intastance whereas the later is having “eager” instantiation. A class having all the data members static is considered as a self contained class and has no dependancies on the outside objects so no more scalable.

Pros

1. Most of the cases it’s one of the best patterns in MVC architecture. Because it unifies the roles of model, view and controller.

2. Useful in impelmenting sessions( this may sound some what like static variables)

3. Very basic and simple pattern to learn.

Cons

1. Singleton approach provides global access point to nearly ervery where it’s accessed- this is so similar to the use of global variables. Better programming says not to use global variables.

2. The classes that use singleton instances are engulfed with the deepndency of the singleton class. Because if one class modifies some values, other class has to bear with the changes. Thus breaking the modularity of your programming.

There can be other pros & cons when we look in to it with different perspectives.

There is another type of design pattern that is verry uncommon in use is the Multiton. Multiton has the same sort of decaration except it keeps track of a specified no of instatination. Think- you need two managers for your organization which serve same duty, sit in same cabin and so on have similar properties. You may need a multiton in this case.

I will be happy if some one let sharing his opinion on my above observations.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>