Sunday, June 20, 2021

C# Design Pattern - Simple Factory Design Pattern



Features of Simple Factory Design Pattern
  1. Simple Factory Design Pattern is a creational design pattern.
  2. Factory is a concrete class in simple factory design pattern which is delegated the task to create different types of objects. What type of object will be created by the factory class is decided by static get method parameter of the factory class. The important point to note is that this method returns an interface type which is inherited by all the classes which objects can be created by the factory class. These different types of objects are child classes of a parent Interface. 
  3. The logic of the object creation is hidden inside the Factory class and it is not exposed to the client class which consumes the objects created by factory class.
  4. The client class demands the object and passes the object type using parameter. The factory class creates the demanded object as per parameter.
  5. Without factory, the client class creates the object but when factory design pattern is followed, the object creation task is delegated by class to the factory class.
  6. Without factory, the client class is tightly coupled with the classes of the objects which the client class creates.
  7. The factory class has a static parameterized method which parameter is used to decide which type of object will be created by factory. 
Example
In the following example, the GetDatabase() method of DatabaseFactory class creates different types of objects but the point to note is that GetDatabase method returns IDatabase interface. This is possible because each created object's class implements IDatabase interface.

using System;
namespace SFDP
{
    interface IDatabase
    {
        void Connect();
    }
    class MySql : IDatabase
    {
        public void Connect()
        {
            Console.WriteLine("Connected with MySql database.");
        }
    }
    class Sql : IDatabase
    {
        public void Connect()
        {
            Console.WriteLine("Connected with Sql database.");
        }
    }
    class Oracle : IDatabase
    {
        public void Connect()
        {
            Console.WriteLine("Connected with Oracle database.");
        }
    }
//factory class
    class DatabaseFactory
    {
        public static IDatabase GetDatabase(string databaseName)
        {
            switch (databaseName.ToLower())
            {
                case "sql":
                    return new Sql();
                case "oracle":
                    return new Oracle();
                case "mysql":
                    return new MySql();
                default:
                    return null;
            }
        }
    }
//client class
    class Program
    {
        static void Main(string[] args)
        {
            IDatabase db = DatabaseFactory.GetDatabase("Oracle");
            if (db != null)
            {
                db.Connect();
            }
            else
            {
                Console.WriteLine("Connection failed...");
            }
        }
    }
}

No comments:

Post a Comment

Hot Topics