Design Patterns


1.     Singleton Design Pattern
·        It ensures a class has only one instance and provides a global point of access to it.
·        Advantages:
o   It can be implement interfaces.
o   It can be also inherit from other classes.
o   It can be lazy loaded.
o   It has Static Initialization.
o   It can be extended into a factory pattern.
o   It provides a single point of access to a particular instance, so it is easy to maintain
·        Disadvantages:
o   Unit testing is more difficult because it introduces a global state into an application
o   It reduces the potential for parallelism, because to access the singleton in a multi-threaded system, an object must be serialized by locking
·        It can be implemented in following ways:
o   No Thread Safe Singleton: Multithreading issue
public sealed class Singleton { 
    private Singleton() { } 
    private static Singleton instance = null; 
    public static Singleton Instance { 
        get { 
            if (instance == null) { 
                instance = new Singleton(); 
            } 
            return instance; 
        } 
    } 
}
o   Thread-Safety Singleton: Performance issue
public sealed class Singleton { 
    Singleton() { } 
    private static readonly object padlock = new object(); 
    private static Singleton instance = null; 
    public static Singleton Instance { 
        get { 
            lock (padlock) { 
                if (instance == null) { 
                    instance = new Singleton(); 
                } 
                return instance; 
            } 
        } 
    } 
}
o   Thread-Safety Singleton using Double-Check Locking
public sealed class Singleton { 
    Singleton() { } 
    private static readonly object padlock = new object(); 
    private static Singleton instance = null; 
    public static Singleton Instance { 
        get { 
            if (instance == null) { 
                lock (padlock) { 
                    if (instance == null) { 
                        instance = new Singleton(); 
                    } 
                } 
            } 
            return instance; 
        } 
    } 
}
o   Thread-Safe Singleton without using locks and no lazy instantiation: Not lazy
public sealed class Singleton { 
    private static readonly Singleton instance = new Singleton();
    static Singleton() { } 
    private Singleton() { } 
    public static Singleton Instance { 
        get { 
            return instance; 
        } 
    } 
}
o   Fully lazy instantiation
public sealed class Singleton {
    private Singleton() { }
    public static Singleton Instance { get { return Nested.instance; } }
    private class Nested {
        static Nested() { }
        internal static readonly Singleton instance = new Singleton();
    }
}
o   Using .NET 4's Lazy<T> type: .NET 4 (or higher)
public sealed class Singleton { 
    private Singleton() { } 
    private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton()); 
    public static Singleton Instance { 
        get { 
            return lazy.Value; 
        } 
    } 
} 
2.     Factory Design Pattern
·        It is a Creational Pattern that simplifies object creation. You need not worry about the object creation; you just need to supply an appropriate parameter and factory to provide you a product as needed.
·        Creating objects without exposing the instantiation logic to the client
·        Referring to the newly created objects through a common interface
·        Example:
public static class FactoryClass {      
    public static IMobile CreateMobileObject(MobileType mobileType) {
        IMobile objIMobile=null;
        switch (mobileType) {
            case MobileType.Samsung:
                objIMobile = new Samsung();
                return objIMobile;
            case MobileType.Apple:
                objIMobile = new Apple();
                return objIMobile;
            case MobileType.Nokia:
                objIMobile = new Nokia();
                return objIMobile;
            default:
                return null;
        }
    }
}

//Sample code of class that implements IMobile interface
public class Samsung : IMobile {
    public string ModelName() {
        return "Samsung Galaxy Grand";
    }
    public string OperatingSystem() {
return "Samsung Uses Android OS For Galaxy Mobile series ";
    }
}

No comments:

Post a Comment