Sunday, June 20, 2021

C# Design Pattern - Abstract Factory Design Pattern





Look at the following hierarchy of teachers.

Abstraction of top level -> Teacher 
Abstraction of middle level -> ScienceTeacher and ArtTeacher
Concrete classes at the bottom level -> PhysicsTeacher, ChemistryTeacher, BiologyTeacher, HindiTeacher, EnglishTeacher

Step1. Top level abstract Class/Interface
All teachers teach. This method will be included in the Teacher, top level abstraction. Abstraction can be as interface or abstract class.

public interface Teacher
    {
        string teach();
    }
Step2. Bottom level all Concrete Classes
public class PhysicsTeacher : Teacher
{
    public string teach()
        {
            return "Physics";
        }
}
public class ChemistryTeacher : Teacher
{
    public string teach()
        {
            return "Chemistry";
        }
}
public class BiologyTeacher : Teacher
{
    public string teach()
        {
            return "Biology";
        }
}
public class HindiTeacher : Teacher
{
    public string teach()
        {
            return "Hindi";
        }
}
public class EnglishTeacher : Teacher
{
    public string teach()
        {
            return "English";
        }
}
Step3. Create top level abstract TeacherFactory
    public abstract class TeacherFactory
    {
        public abstract Teacher getTeacher(string TeacherType);
        public static TeacherFactory CreateTeacherFactory(string FactoryType)
        {
            if (FactoryType.Equals("science"))
                return new ScienceTeacherFactory();
            else if (FactoryType.Equals("art"))
                return new ArtTeacherFactory();
            else return null;
        }
    }
Thus, at the top level abstraction, factory will be TeacherFactory. This will return Teacher as per their type (parameter will decide) and similarly it will have static method CreateTeacherFactory which will return either ScienceTeacherFactory or ArtTeacherFactory.

Step4. Create mid level all concrete factory classes which will inherit top level abstract TeacherFactory

    public class ScienceTeacherFactory : TeacherFactory
    {
        public override Teacher getTeacher(string TeacherType)
        {
            switch (TeacherType)
            {
                case "physics": return new PhysicsTeacher();
                case "chemistry": return new ChemistryTeacher();
                case "biology": return new BiologyTeacher();
                default: return null;
            }
        }

    }
    public class ArtTeacherFactory : TeacherFactory
    {
        public override Teacher getTeacher(string TeacherType)
        {
            switch (TeacherType)
            {
                case "hindi": return new HindiTeacher();
                case "english": return new EnglishTeacher();
                default: return null;
            }
        }
    }
   
Step5. Client class will consume the classes.
 class Program
    {
        static void Main(string[] args)
        {
            Teacher teacher = null;
            TeacherFactory teacherFactory = null;
            teacherFactory = TeacherFactory.CreateTeacherFactory("science");
            Console.WriteLine("Teacher Factory type : " + teacherFactory.GetType().Name);
            teacher = teacherFactory.getTeacher("physics");
            Console.WriteLine("Teacher Type: " + teacher.GetType().Name);
            Console.WriteLine("Teaches: " + teacher.teach());

            teacherFactory = TeacherFactory.CreateTeacherFactory("art");
            Console.WriteLine("Teacher Factory type : " + teacherFactory.GetType().Name);
            teacher = teacherFactory.getTeacher("hindi");
            Console.WriteLine("Teacher Type: " + teacher.GetType().Name);
            Console.WriteLine("Teaches: " + teacher.teach());
            Console.ReadLine();

        }
    }
}
Complete Code
using System;
namespace AFC
{
    public interface Teacher
    {
        string teach();
    }
    public class PhysicsTeacher : Teacher
    {
        public string teach()
        {
            return "Physics";
        }
    }
    public class ChemistryTeacher : Teacher
    {
        public string teach()
        {
            return "Chemistry";
        }
    }
    public class BiologyTeacher : Teacher
    {
        public string teach()
        {
            return "Biology";
        }
    }
    public class HindiTeacher : Teacher
    {
        public string teach()
        {
            return "Hindi";
        }
    }
    public class EnglishTeacher : Teacher
    {
        public string teach()
        {
            return "English";
        }
    }

    public abstract class TeacherFactory
    {
        public abstract Teacher getTeacher(string TeacherType);
        public static TeacherFactory CreateTeacherFactory(string FactoryType)
        {
            if (FactoryType.Equals("science"))
                return new ScienceTeacherFactory();
            else if (FactoryType.Equals("art"))
                return new ArtTeacherFactory();
            else return null;
        }
    }
    public class ScienceTeacherFactory : TeacherFactory
    {
        public override Teacher getTeacher(string TeacherType)
        {
            switch (TeacherType)
            {
                case "physics": return new PhysicsTeacher();
                case "chemistry": return new ChemistryTeacher();
                case "biology": return new BiologyTeacher();
                default: return null;
            }
        }

    }
    public class ArtTeacherFactory : TeacherFactory
    {
        public override Teacher getTeacher(string TeacherType)
        {
            switch (TeacherType)
            {
                case "hindi": return new HindiTeacher();
                case "english": return new EnglishTeacher();
                default: return null;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Teacher teacher = null;
            TeacherFactory teacherFactory = null;
            teacherFactory = TeacherFactory.CreateTeacherFactory("science");
            Console.WriteLine("Teacher Factory type : " + teacherFactory.GetType().Name);
            teacher = teacherFactory.getTeacher("physics");
            Console.WriteLine("Teacher Type: " + teacher.GetType().Name);
            Console.WriteLine("Teaches: " + teacher.teach());

            teacherFactory = TeacherFactory.CreateTeacherFactory("art");
            Console.WriteLine("Teacher Factory type : " + teacherFactory.GetType().Name);
            teacher = teacherFactory.getTeacher("hindi");
            Console.WriteLine("Teacher Type: " + teacher.GetType().Name);
            Console.WriteLine("Teaches: " + teacher.teach());
            Console.ReadLine();

        }
    }
}


 

No comments:

Post a Comment

Hot Topics