Sunday, 8 August 2021

Singleton Pattern

Singleton Pattern comes under creational design patterns category, this pattern lets you define a class that has only one instance, while providing a global access to this created instance. As singleton is a class designed to only ever have one instance.

Singleton instance example

  • Access to file system.
  • Access to shared network resource
  • Expensive one-time configuration

Singleton Structure

  • Singleton instance which is private
  • Singleton public method/function to access singleton object.
  • Private constructor
  • Other methods and properties

Singleton Features

  • At any time, only 0 or 1 instance of Singleton class exists in the application.
  • Singleton classes are created without parameters.
  • Assume lazy instantiation as the default.
  • A single, private, parameter-less constructor and class should be sealed.
  • A private static field holds the only reference to the instance.
  • A public static method provides access to this field.

Singleton Implementation in C#

  • Naïve Singleton - Not thread safe but provide performance
  • Thread Safe Singleton - Thread safe will slow down performance. Double check thread safe singleton  pattern used to optimize the performance.
  • Leveraging Static Constructors - C# static constructors only run once per app domain. Are called  when any static member of a type is referenced. Make sure you use an explicit static constructor to avoid issue with c# compiler and before field init. (https://csharpindepth.com/articles/BeforeFieldInit)
  • Lazy<T> - Lazy<T> was introduced in .Net 4, provides built-in support for lazy initialization. Specify a means of creating Type. 

Singleton Pattern is also consider as Antipattern.

  • Difficult to test due to shared state.
  • Doesn't follow Separation of Concerns.
  • Doesn't follow single responsibility
  • Doesn't follow DRY
  • Better alternative exist.

Singletons vs. Static Classes

Singleton

  • Can implement interfaces
  • Can be passed as an argument
  • Can be assigned to variables
  • Support polymorphism
  • Can have state
  • Can be serialized

Static Classes

  • No interfaces
  • Cannot be passed as arguments
  • Cannot be assigned
  • Purely procedural
  • Can only access global state
  • No support for serialization

Singletons Behavior Using Containers (IoC)

  • You can use dependency injection tool/library to inject dependency.
  • Container manages instance lifetime
  • .Net core has inbuilt support for dependency injection using service provide.

Prototype Pattern

Prototype Pattern comes under creational design patterns category, this pattern lets you copy/cloning existing object instead of creating new one and also be customized as per the requirement.

Builder Pattern

Builder Pattern comes under creational design patterns category, this pattern lets you construct complex objects step by step approach.

Abstract Factory Pattern

Abstract Factory Pattern comes under creational design patterns category, this pattern lets you create families of related or dependent objects without specifying their concrete class.


Factory Method Pattern

Factory method pattern comes under creational design patterns category, this pattern provides one of the best ways to create an object. This pattern provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.

Tuesday, 28 February 2017

Polymorphism in OOP

"Polymorphism" is a greek word, "poly" means many and "morph" means forms.
In object oriented programming paradigm Polymorphism is expressed as one object behaving as multiple forms. One function behave in different forms.

Inheritance in OOP

Inheritance

One of the most important concepts of object oriented programming is Inheritance. Inheritance is a mechanism of acquiring features and behaviors of one class called as base or parent class into another class called as derived class.

Inheritance implements a IS-A relationship.

Advantages of inheritance

  • Code re-usability 
  • Reduction in code size
  • Improve code readability
  • Code extensible support by overriding base class method into derived class  

Disadvantage of inheritance

  • Tight coupling between base class and child class
  • Performance of your program is affected if inheritance is not implemented properly.
  • Many unused data members remain unused & memory is allocated for the same.

Different types of inheritance

  • Single Inheritance

When a derived class is created from a single base class.
C# Example
public class Parent
{
    //Definition
}

public class Derived : Parent
{
    //Definition
}

    •  Multi-Level Inheritance

    When a derived class is created from another derived class is called as multi-level inheritance
    C# Example
    ppublic class Parent
    {
        //Definition
    }

    public class Derived1 : Parent
    {
        //Definition
    }

    public class Derived2 : Derived1
    {
        //Definition
    }

    • Multiple Inheritance

    When a derived class is created from more than one class then it is called as multiple inheritance. This type of inheritance is not supported in C# or .Net
    This type of inheritance is not supported in C# or .Net

    • Hierarchical Inheritance

    When a multiple derived class is created from one single class then it is called as Hierarchical inheritance.
     C# Example
    public class Parent
    {
        //Definition
    }

    public class Derived1 : Parent
    {
        //Definition
    }

    public class Derived2 : Parent
    {
        //Definition
    }

    public class Derived3 : Derived1
    {
        //Definition
    }

    public class Derived4 : Derived1
    {
        //Definition
    }

    public class Derived5 : Derived2
    {
        //Definition
    }

    public class Derived6 : Derived2
    {
        //Definition
    }

    • Hybrid Inheritance

    This is combination of more than one inheritance. It may be a combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or
    Hierarchical and Multilevel and Multiple inheritance.

    • Why C# does not support multiple inheritance?

    C# does not support multiple inheritance because of diamond problem. To know more about diamond problem please refer below link 
    https://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem

    • As we know multiple inheritance is not supported by C# or .Net what is the other way to achieve multiple inheritance in C# or .Net? 

    We can achieve similar functionality using interface in C#.

    • Is static class can be inherited ?

    Static class can not be inherited. C# complies static class as sealed abstract class hence static class cannot be inherited in c#.

    • How to make normal class non inheritable c#?

    Using sealed keyword in c# we can stop class from further inheriting it.

    Custome Service Class to connect wcf service

      using  System; using  System.ServiceModel; namespace Custom.Service.Client {      // Borrowed from: http://old.iserviceoriented.com/blog/p...