Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
.NET Design Patterns

You're reading from   .NET Design Patterns Learn to Apply Patterns in daily development tasks under .NET Platform to take your productivity to new heights.

Arrow left icon
Product type Paperback
Published in Jan 2017
Publisher Packt
ISBN-13 9781786466150
Length 314 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Praseed Pai Praseed Pai
Author Profile Icon Praseed Pai
Praseed Pai
Shine Xavier Shine Xavier
Author Profile Icon Shine Xavier
Shine Xavier
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. An Introduction to Patterns and Pattern Catalogs FREE CHAPTER 2. Why We Need Design Patterns? 3. A Logging Library 4. Targeting Multiple Databases 5. Producing Tabular Reports 6. Plotting Mathematical Expressions 7. Patterns in the .NET Base Class Library 8. Concurrent and Parallel Programming under .NET 9. Functional Programming Techniques for Better State Management 10. Pattern Implementation Using Object/Functional Programming 11. What is Reactive Programming? 12. Reactive Programming Using .NET Rx Extensions 13. Reactive Programming Using RxJS 14. A Road Ahead

C# language and the singleton pattern

The authors consider the singleton pattern, the way it was presented in the GoF book, as some kind of anti-pattern. A lot has been written about how to implement it in a multi-core/multi-threaded environment. Constructs such as the double-checked locking pattern have been implemented to incorporate lazy loading while implementing singleton.

The C# programming language has got a nifty feature called a static constructor, which helps to implement the singleton pattern in a thread-safe manner. The static constructor is guaranteed to be called before any method (including the constructor) is called. We believe we can stop cutting down trees in order to write about the singleton pattern, at least in the .NET world.

    //--Chap1_06.cs 
    using System; 
 
    class SingleInstance 
    { 
      private int value = 10; 
      //----- In the case of Singleton Pattern, we make our 
      //----- ctor private to avoid instantiating the object using 
      //----- the new keyword 
      private SingleInstance() { } 
 
      //----- The static method acts as a mechanism to expose 
      //------ the internal instance 
      public static SingleInstance Instance {  
        get {  
          return Nested.instance; 
        }  
      } 
 
      private class Nested 
      { 
        static Nested() { } 
        internal static readonly SingleInstance instance 
        = new SingleInstance(); 
      } 
      public void Increment() 
      { 
        value++; 
      } 
      public int Value { get { return value; } } 
    } 
 
    public class SingletonExample 
    { 
      public static void Main(String[] args) 
    { 
      SingleInstance t1 = SingleInstance.Instance; 
      SingleInstance t2 = SingleInstance.Instance; 
      t1.Increment(); 
      if (t1.Value == t2.Value) 
        Console.WriteLine("SingleTon Object"); 
    } 
  } 
You have been reading a chapter from
.NET Design Patterns
Published in: Jan 2017
Publisher: Packt
ISBN-13: 9781786466150
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime