Technology

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

By, administrator
  • 20 Sep, 2024
  • 227 Views
  • 0 Comment

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.

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,

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.

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.

Tags:

Leave a comment

Your email address will not be published. Required fields are marked *