Location>code7788 >text

Multiple implementations of the C# singleton pattern

Popularity:597 ℃/2024-11-07 07:59:57

Introduction to the Singleton Pattern

The singleton pattern is a creation-based design pattern that essentially ensures that there is only one instance of a class and provides a global access point to that instance. In C#, there are several ways to implement the singleton pattern, each with its own specific usage scenarios and considerations.

The Role of Design Patterns

  • Improve code reusability: By defining a standard set of solutions, design patterns enable the same or similar problems to reuse the same code structure or logic across different projects.
  • Enhance code readability: Design patterns express complex code logic in a clear, concise way, making it easier for other developers to understand and maintain the code.
  • Improve the maintainability of the system: the design pattern follows certain design principles, such as the principle of opening and closing, the principle of Richter's substitution, etc., which help to reduce the coupling of the various parts of the system and improve the scalability and maintainability of the system.

Hungry Man Singleton Pattern

A starved singleton is an instance that is created when the class is loaded. The advantage is that it is simple to implement, the disadvantage is that it wastes resources if the instance is not used.

        /// <summary>
/// Hungry Man Singleton Pattern
        /// </summary>
        public class SingletonEager
        {
            private SingletonEager() { }

            private static readonly SingletonEager _instance = new SingletonEager();

            public static SingletonEager Instance
            {
                get { return _instance; }
            }

            public void DoSomething()
            {
("Hungry man style singleton pattern...") )
            }
        }

lazy man's singleton pattern

Lazy singletons create instances only when they are accessed for the first time. For thread-safety, a locking mechanism is usually required.

        /// <summary>
/// Lazy Man's Singleton Pattern
        /// </summary>
        public class SingletonLazy
        {
            private SingletonLazy() { }

            private static SingletonLazy? _instance;

            private static readonly object _lockObj = new object();

            public static SingletonLazy Instance
            {
                get
                {
                    if (_instance == null)
                    {
                        lock (_lockObj)
                        {
                            if (_instance == null)
                            {
                                _instance = new SingletonLazy();
                            }
                        }
                    }
                    return _instance;
                }
            }

            public void DoSomething()
            {
("The lazy man's singleton pattern...") ;
            }
        }

Lazy Loading Singleton Pattern

If you are using .NET 4 (or later), you can use the Lazy class to implement a thread-safe, lazy-loaded singleton pattern.

        /// <summary>
/// Lazy loading singleton pattern
        /// </summary>
        public sealed class SingletonByLazy
        {
            private static readonly Lazy<SingletonByLazy> _lazy = new Lazy<SingletonByLazy>(() => new SingletonByLazy());

            public static SingletonByLazy Instance { get { return _lazy.Value; } }

            private SingletonByLazy() { }

            public void DoSomething()
            {
("Lazy loading singleton pattern.") ;
            }
        }

Design Patterns Tutorial

/s/FM0ThUR92EcXJ3YY313ifw