Replace Conditional Statements (IF/ELSE Or SWITCH) With Factory

Introduction

In this article, we will learn how we can replace long IF/ELSE or long SWITCH (long conditional statement) with polymorphism. This is also called RIP design pattern. RIP means Replace If with Polymorphism design pattern

Tools used for development

  • Visual Studio 2019

Long condition statement

In the below code snippet we can see a skill set which is suitable for a job opening in our company with a long condition statement, (i.e, switch statement we have used). Currently we have only two requirements. If we need to add more skills to switch statement then the conditions will keep on increasing.

class Program  
{  
    static void Main(string[] args)  
    {  
        Console.WriteLine("Enter your skill set for job opening (like JavaScript,c#,Net)");  
        string knowledge = Console.ReadLine();  
        switch (knowledge.ToLower())  
        {  
            case "javascript":  
                Console.WriteLine("Requirement matches");  
                break;  
            case "c#":  
                Console.WriteLine("Requirement matches");  
                break;  
            default:  
                Console.WriteLine("Requirement does not matches");  
                break;  
        }  
  
        Console.ReadKey();  
  
    }  
}  

Replacing condition statement with polymorphism

We will create a Simple Factory design pattern, with class name as SimpleFactory. We will create a skill Dictionary and in constructor add all the skill sets and return the value, as shown in below code snippet.

class Program  
   {  
       static void Main(string[] args)  
       {  
           Console.WriteLine("Enter your skill set for job opening (like JavaScript,c#,Net)");  
           string knowledge = Console.ReadLine();  
           Console.WriteLine(SimpleFactory.Create(knowledge.ToLower()));  
           Console.ReadKey();  
       }  
   }  
   public class SimpleFactory  
   {  
       private static Dictionary<string, string> skill = new Dictionary<string, string>();  
       static SimpleFactory()  
       {  
           skill.Add("javascript", "Requirement matches");  
           skill.Add("c#", "Requirement matches");  
       }  
       public static string Create(string CustType)  
       {  
           // Design Pattern : RIP Pattern  
           return skill[CustType];  
       }  
   }  

Implementing lazy loading

In the above code, we have seen how we replace condition statement with Dictionary. Now we will see how we implement lazy loading.

public static class SimpleFactory
    {  
        private static Dictionary<string, string> skills= new Dictionary<string, string>();  
  
        public static string Create(string skillType)  
        {  
            if (skills.Count == 0)  
            {  
                skills.Add("javascript", "Requirement matches");  
                skills.Add("c#", "Requirement matches");  
            }  
            // Design Pattern : RIP Pattern  
            return skills[skillType];  
        }  
    }  

Implementing lazy loading using Lazy class

In the above code, we have seen how we used lazy loading. Now we will see how we implement lazy loading using Lazy class from .Net framework.

class Program  
   {  
       static void Main(string[] args)  
       {  
           Console.WriteLine("Enter your skill set for job opening (like JavaScript,c#,Net)");  
  
           string knowledge = Console.ReadLine();  
        
           Console.WriteLine(SimpleFactory.Create(knowledge.ToLower()));  
  
           Console.ReadKey();  
  
       }  
   }  
  
   public static class SimpleFactory  
   {  
       private static Lazy<Dictionary<string, string>> skill = null;  
  
       static SimpleFactory()  
       {  
           skill = new Lazy<Dictionary<string, string>>(() => LoadCustomer());  
       }  
  
       private static Dictionary<string, string> LoadCustomer()  
       {  
           Dictionary<string, string> temp = new Dictionary<string, string>();  
  
           temp.Add("javascript", "Requirement matches");  
           temp.Add("c#", "Requirement matches");  
  
           return temp;  
       }  
  
       public static string Create(string skillType)  
       {  
           // Design Pattern : RIP Pattern  
           return skill.Value[skillType];  
       }  
   }  

Summary

In this article, we have learned how we can replace long IF/ELSE or long SWITCH (long conditional statement) with polymorphism.


Similar Articles