Design Patterns Defined


What is design pattern?
Design patterns are recurring solutions for the recurring problems. According to me design is blue print or sketch of something so it can be defined as creation of something in mind. Moving to pattern, we can define it as guideline, or something that repeats. Now the definition of design pattern becomes creating something in mind that repeats or in other words capturing design ideas as a "pattern" to the problems.
Advantages:
·      Speed up the development process by providing tested and proven development process.
·     Reusing design patterns helps to prevent issues that can cause major problems
           Common programming problem enable large scale reuse of S/W

Have a look on one of the easiest pattern

Singleton
namespace DesignPatterns




{

    public sealed class Singleton
    {

   private static readonly Singleton  instance = new Singleton();
        private Singleton()
        {

        }

        public static Singleton Instance

        {

            get

            {

                return instance;

            }

        }

    }



}
 
   Thread-Safe Singleton Code
 
namespace DesignPatterns
{

    public sealed class MTSingleton

    {
        private static volatile MTSingleton    instance = null;
        private static object syncRoot = new object();
        private MTSingleton()
        {
        }

        public static MTSingleton Instance
        {

            get 
            {
                if (instance == null)
                {
                    lock(syncRoot)
                    {
                        if (instance == null)
                            instance = new MTSingleton();
                    }
                }
                return instance;
            }
 
1.  The instance member variable is no longer set at creation time, but in the Instance property instead. The instance variable is declared to be volatile in order to assure that the assignment of instance complete before the instance can be accessed. 
2.       The syncRoot object is used to lock on. The lock ensures that only one thread can access the instance creation code at once. That way, you won't get into a situation where two different threads are trying to create the singleton simultaneously. 
3.       We double-check the existence of the instance variable within the locked code to be sure that exactly one instance is ever created. 
Keyword Volatile:
Whenever a volatile is requested, the system returns the current value at the time of the request. All assignments are written to the object immediately.

Common usage of the volatile modifier is when a particular field is accessed by many threads without using the lock statement to serialize access. So in essence the volatile modifier guarantees that a thread will retrieve the most recent value written by another thread (even if it was modified by the previous instruction from you call).

You are not allowed to use volatile on just any time. The following is a list of types you can implement this modifier on:
  • Any reference type.
  • Any pointer type in a unsafe context
  • sbyte, byte, short, ushort, int, uint, char, float, bool.
  • An enum type with an enum base type of the following: byte, sbyte, short, ushort, int, uint.




 

Comments

Popular posts from this blog

MVC Defined

Azure Kubernetes Service (AKS)