Showing posts with label Factory Design pattern in C#. Show all posts
Showing posts with label Factory Design pattern in C#. Show all posts

Saturday, November 21, 2015

Avoid multiple If-else Or Switch in decision making flow.

Sometime multiple if-else or switch can increase code smell. It’s always Good practice to avoid those to decrease confusion and increase code readability.

In this article we will implement some pattern which can replace simple if-else or switch case. Let’s take one small example to implement same.

    public interface IMessage
    {
        void Send();
    }

    public class SMS : IMessage
    {
        public void Send()
        {
            System.Console.WriteLine("SMS Sending..");
        }
    }
    public class Mail : IMessage
    {
        public void Send()
        {
            System.Console.WriteLine("Mail Sending..");
        }
    }

We have implement IMessage in SMS and Mail classes. Now, here is an Enum to choose the Message type.

enum MessageType { Sms, Mail };

Here is the actual logic to call appropriate class.

      public static void Main(String[] args)
        {
            Dictionary<MessageType, IMessage> ObjectFactory =
new Dictionary<MessageType, IMessage>
            {
                { MessageType.Sms, new SMS() },
                { MessageType.Mail, new Mail() }
            };

            System.Console.WriteLine("0: For SMS, 1: Mail");

            int ReadType = Convert.ToInt32(
            System.Console.ReadLine());

            ObjectFactory[(MessageType)ReadType].Send();
           

            System.Console.ReadLine();
        }

The implementation is very simple one. Just we are maintaining one dictionary by containing object of concrete implementation of the IMessage interface.

Once user input some value, we are getting appropriate object and calling respective function. Here is Full code implementation with output.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Console
{
    public interface IMessage
    {
        void Send();
    }

    public class SMS : IMessage
    {
        public void Send()
        {
            System.Console.WriteLine("SMS Sending..");
        }
    }
    public class Mail : IMessage
    {
        public void Send()
        {
            System.Console.WriteLine("Mail Sending..");
        }
    }

    enum MessageType { Sms, Mail };
    class Program
    {
        public static void Main(String[] args)
        {
            Dictionary<MessageType, IMessage> ObjectFactory 
                  = new Dictionary<MessageType, IMessage>
            {
                { MessageType.Sms, new SMS() },
                { MessageType.Mail, new Mail() }
            };

            System.Console.WriteLine("0: For SMS, 1: Mail");

            int ReadType = Convert.ToInt32(
            System.Console.ReadLine());
            ObjectFactory[(MessageType)ReadType].Send();
           

            System.Console.ReadLine();
        }
    }
}


Friday, June 14, 2013

Factory Design pattern

Factory design pattern is one of the common design pattern  in software project. Let’s understand the basic of Factory design patern.
Before start discussion Let’s learn the advantage of Factory design pattern.  And obviously those are the reason to implement Factory design patern.

1)      In client here and there we create object using new keyword within much more if – else condition . If you implement factory design pattern to reduce new keyword in client.

2)      When you want to abstract your business class from client ,then implement factory pattern in your project in project.

In below code it will show how to implement factory design pattern in project. Here I have two types of employee class in my code and I have created a factory class EmployeeFactory to supply object of appropriate class depending upon class request . Now if you add one more type of employee class in your nosiness logic the client should not bother at all. Just tell your client to send proper code to get object of newly added class.
So basically our factory class is making am abstraction over our business class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;

namespace BlogProject
{
    // Factory Design patern
    public interface IEmployee
    {
        void ShowEmployee();
    }
    public class Developer:IEmployee
    {
        public void ShowEmployee()
        {
            Console.Write("I ma .NET Developer");
        }
    }

    public class HR : IEmployee
    {
        public void ShowEmployee()
        {
            Console.Write("I am Technical HR");
        }
    }

    public class EmployeeFactory
    {
        // Factory class to generate object of concrite class
        public IEmployee GetObject(int a)
        {
            if (a == 1)
            {
                return new Developer();
            }
            else if (a == 2)
            {
                return new HR();
            }
            else
                return null;
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter 1 to create Developer class object and 2 to create HR class object"+"\n");
            int Type = Convert.ToInt32(Console.ReadLine());
            EmployeeFactory objFactory = new EmployeeFactory();
            IEmployee objemp = objFactory.GetObject(Type);
            objemp.ShowEmployee();
            Console.ReadLine();
        }
    }
}