The Artima Developer Community
Sponsored Link

.NET Buzz Forum
The Singleton

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jonathan Crossland

Posts: 630
Nickname: jonathanc
Registered: Feb, 2004

Jonathan Crossland is a software architect for Lucid Ocean Ltd
The Singleton Posted: Nov 23, 2004 9:12 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Jonathan Crossland.
Original Post: The Singleton
Feed Title: Jonathan Crossland Weblog
Feed URL: http://www.jonathancrossland.com/syndication.axd
Feed Description: Design, Frameworks, Patterns and Idioms
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Jonathan Crossland
Latest Posts From Jonathan Crossland Weblog

Advertisement


From the Inbox: How do I code a singleton pattern?

I usually implement singletons in the following three ways.

The three implementations depend one or more usage requirements. They in turn use the Instance, GetInstance and CreateInstance as instance creators.


  // I used this the most often
    public class Singleton1
    {
        public static readonly Singleton1 Instance = new Singleton1();
        
        private Singleton1(){}

        private string _TestProperty;
        public string TestProperty
        {
            get{return _TestProperty;}
            set{_TestProperty = value;}
        }
    }

    //using Method and private static field
    public class Singleton2
    {
        private static Singleton2 _Instance;
        
        private Singleton2(){}

        public static Singleton2 GetInstance()
        {
            if (_Instance==null)
                _Instance = new Singleton2();

            return _Instance;
        }

        private string _TestProperty;
        public string TestProperty
        {
            get{return _TestProperty;}
            set{_TestProperty = value;}
        }
    }
    
    // Created by Singleton3Factory
    public class Singleton3
    {
        protected internal Singleton3(){}
    
        private string _TestProperty;
        public string TestProperty
        {
            get{return _TestProperty;}
            set{_TestProperty = value;}
        }
    }

    //creator of the Singleton3 object
    public class Singleton3Factory
    {

        private static Singleton3 _Instance;

        public static Singleton3 CreateInstance()
        {
            
            if (_Instance==null)
                _Instance = new Singleton3();
            
            return _Instance;
        }
    }


Read: The Singleton

Topic: The Self-Importance Factor Previous Topic   Next Topic Topic: Language Oriented Programming.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use