Gang of Four (GOF) Design Patterns in .NET

source article: http://www.dotnet-tricks.com/Tutorial/designpatterns/NTEH250513-Gang-of-Four-(GOF)-Design-Patterns-in-.Net-.html

  1. Creational Design Patterns

    1. Factory Method : Create instances of derived classes
    2. Abstract Factory : Create instances of several classes belonging to different families
    3. Builder : Separates an object construction from its representation
    4. Prototype : Create a duplicate object or clone of the object
    5. Singleton : Ensures that a class can has only one instance
  2. Structural Design Patterns

    1. Adapter : Match interfaces of different classes
    2. Bridge : Separates an object’s abstraction from its implementation
    3. Composite : A tree structure of simple and composite objects
    4. Decorator : Add responsibilities to objects dynamically
    5. Façade : A single class that represents an entire complex system
    6. Flyweight : Minimize memory usage by sharing as much data as possible with similar objects
    7. Proxy : Provides a surrogate object, which references to other object
  3. Behavioral Design Patterns

    1. Chain of Responsibility
    2. Command
    3. Interpreter
    4. Iterator
    5. Mediator
    6. Memento
    7. Observer
    8. State
    9. Strategy
    10. Visitor
    11. Template Method

Singleton Design Pattern – C#

source article: http://www.dotnet-tricks.com/Tutorial/designpatterns/L2KL080613-Singleton-Design-Pattern—C#.html

 

  1. //eager initialization of singleton
  2. public class Singleton
  3. {
  4.   private static Singleton instance =  new Singleton();
  5.   private Singleton() { }
  6.   public static Singleton GetInstance
  7.   {
  8.      get { return instance; }
  9.    }
  10.  }
  11. }
  12. //lazy initialization of singleton
  13. public class Singleton
  14. {
  15.   private static Singleton instance = null;
  16.   private Singleton() { }
  17.   public static Singleton GetInstance
  18.   {
  19.      get
  20.      {
  21.          if (instance == null)
  22.              instance = new Singleton();
  23.          return instance;
  24.        }
  25.    }
  26. }
  27.  
  28. //Thread-safe (Double-checked Locking) initialization of singleton
  29. public class Singleton
  30. {
  31.    private static Singleton instance = null;
  32.    private Singleton() { }
  33.    private static object lockThis = new object();
  34.    public static Singleton GetInstance
  35.   {
  36.     get
  37.     {
  38.         lock (lockThis)
  39.         {
  40.             if (instance == null)
  41.                 instance = new Singleton();
  42.             return instance;
  43.         }
  44.     }
  45.   }
  46. }