Geeks With Blogs
Moez Mousavi

Design Patterns sounds like an Italian recipies. If you you tell your grand mother that you cooked Bolognese and Pasta for evening, she wouldn't ask you how, as the recipie is famous enough to make scence to everyone. She wouldn't expect a creamy pasta with chicken unless you decided to make her surprise. 

Same story when you coding. In fact the benefit of using Design Patterns is that when you say, for example, you have implemented Singleton, your work mate would have a picture of the code on spot and its make everyone's life much more easier.

One of the simplest one to start is Singleton. The usage is when you want to have a single instance of a class in the entire solution:

 

    public class Singleton

    {

        private static Singleton instance;//Static variable to hold single instance

        //Other variables...

   

        //Private Constructor. So only Singleton can instantiate itself

        private Singleton() { }

 

        //Lazy instantiation (Never get created if doesn't needed)

        public static Singleton Instance

        {

            get

            {

                if (instance == null)

                {

                    instance = new Singleton();

                }

                return instance;

            }

        }

        //Other Methods...

    }

Posted on Sunday, July 10, 2011 1:04 PM C# | Back to top

Copyright © MoezMousavi | Powered by: GeeksWithBlogs.net