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. 
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         Console.WriteLine("Enter your skill set for job opening (like JavaScript,c#,Net)");  
  6.   
  7.         string knowledge = Console.ReadLine();  
  8.         switch (knowledge.ToLower())  
  9.         {  
  10.             case "javascript":  
  11.                 Console.WriteLine("Requirement matches");  
  12.                 break;  
  13.             case "c#":  
  14.                 Console.WriteLine("Requirement matches");  
  15.                 break;  
  16.             default:  
  17.                 Console.WriteLine("Requirement does not matches");  
  18.                 break;  
  19.         }  
  20.   
  21.         Console.ReadKey();  
  22.   
  23.     }  
  24. }  

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,
  1. class Program  
  2.    {  
  3.        static void Main(string[] args)  
  4.        {  
  5.            Console.WriteLine("Enter your skill set for job opening (like JavaScript,c#,Net)");  
  6.   
  7.            string knowledge = Console.ReadLine();  
  8.          
  9.            Console.WriteLine(SimpleFactory.Create(knowledge.ToLower()));  
  10.   
  11.            Console.ReadKey();  
  12.        }  
  13.    }  
  14.   
  15.    public class SimpleFactory  
  16.    {  
  17.        private static Dictionary<stringstring> skill = new Dictionary<stringstring>();  
  18.   
  19.        static SimpleFactory()  
  20.        {  
  21.            skill.Add("javascript""Requirement matches");  
  22.            skill.Add("c#""Requirement matches");  
  23.        }  
  24.   
  25.        public static string Create(string CustType)  
  26.        {  
  27.            // Design Pattern : RIP Pattern  
  28.            return skill[CustType];  
  29.        }  
  30.    }  

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.
  1. public static class SimpleFactory
  2.     {  
  3.         private static Dictionary<stringstring> skills= new Dictionary<stringstring>();  
  4.   
  5.         public static string Create(string skillType)  
  6.         {  
  7.             if (skills.Count == 0)  
  8.             {  
  9.                 skills.Add("javascript""Requirement matches");  
  10.                 skills.Add("c#""Requirement matches");  
  11.             }  
  12.             // Design Pattern : RIP Pattern  
  13.             return skills[skillType];  
  14.         }  
  15.     }  

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.
  1. class Program  
  2.    {  
  3.        static void Main(string[] args)  
  4.        {  
  5.            Console.WriteLine("Enter your skill set for job opening (like JavaScript,c#,Net)");  
  6.   
  7.            string knowledge = Console.ReadLine();  
  8.         
  9.            Console.WriteLine(SimpleFactory.Create(knowledge.ToLower()));  
  10.   
  11.            Console.ReadKey();  
  12.   
  13.        }  
  14.    }  
  15.   
  16.    public static class SimpleFactory  
  17.    {  
  18.        private static Lazy<Dictionary<stringstring>> skill = null;  
  19.   
  20.        static SimpleFactory()  
  21.        {  
  22.            skill = new Lazy<Dictionary<stringstring>>(() => LoadCustomer());  
  23.        }  
  24.   
  25.        private static Dictionary<stringstring> LoadCustomer()  
  26.        {  
  27.            Dictionary<stringstring> temp = new Dictionary<stringstring>();  
  28.   
  29.            temp.Add("javascript""Requirement matches");  
  30.            temp.Add("c#""Requirement matches");  
  31.   
  32.            return temp;  
  33.        }  
  34.   
  35.        public static string Create(string skillType)  
  36.        {  
  37.            // Design Pattern : RIP Pattern  
  38.            return skill.Value[skillType];  
  39.        }  
  40.    }  

Summary

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


Similar Articles