Skip to main content

C# Generic Factory

Implement Factory pattern using generics


   public interface IDoWork
  {
      string DoWork();
  }

    Declare an Interface first by abstracting the common  functionality
   Here I am taking the example of DoWork

   public class Manager : IDoWork
  {
    public string DoWork()
    {
        return "Manager Manages School";
    }
  }

    Implement the IDoWork in concrete classes as shown 

    public class Teacher : IDoWork
    {
        public string DoWork()
        {
            return "Teacher teaches student in school";
        }
    }

    public class Student : IDoWork
    {
        public string DoWork()
        {
            return "Study in school";
        }
    }

This class will actually create an instance of the concrete class so that you can work on that        object further

    public class FactoryDemo
    {
        public T CreateFactroy<T>() where T : class
        {
            var type = typeof(T);
            return Activator.CreateInstance(type) as T;
        }
    }

In client you can test as shown below

         static void Main(string[] args)
        {
            //bool isWindows = true;
            //var res = DBConnect(isWindows);
            //Console.WriteLine(" Connection - " + res.ToString());
            FactoryDemo create = new FactoryDemo();
            IDoWork obj = create.CreateFactroy<Teacher>();
            Console.WriteLine(obj.DoWork());

            obj = create.CreateFactroy<Student>();
            Console.WriteLine(obj.DoWork());

            obj = create.CreateFactroy<Manager>();
            Console.WriteLine(obj.DoWork());

            Console.ReadLine();
        }

Comments

  1. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article.. dot net training and placement in chennai | best dot net training in chennai

    ReplyDelete
  2. After exploring a handful of the articles on your website, I seriously like your way of writing a blog. I bookmarked it to my bookmark site list and will be checking back in the near future. Take a look at my web site too and let me know how you feel.
    Click here to get More information.

    ReplyDelete
  3. This is an awesome post.Really very informative and creative contents about Java. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.


    Java training in Chennai

    Java training in Bangalore

    Java training in Hyderabad

    Java Training in Coimbatore

    Java Online Training

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete

Post a Comment

Popular posts from this blog

SQL query - Table sorting

In this article we will learn how to sort the table values with out using dynamic query to improve performance. CREATE PROCEDURE SortingExample ( @sortFiled INT, @sortType INT  -- 1 for ASC, 2 for DESC ) AS BEGIN -- Create a table variable to store user data DECLARE @myTable TABLE ( UserID INT IDENTITY(1,1), UserName VARCHAR(50), Password VARCHAR(50), Email VARCHAR(50) ) -- Insert some data to table to work on that data INSERT INTO @myTable(UserName, Password, Email) VALUES ('Jack', 'JackPwd', 'jack@gmail.com') INSERT INTO @myTable(UserName, Password, Email) VALUES ('Anand', 'AnandPwd', 'raj@gmail.com') INSERT INTO @myTable(UserName, Password, Email) VALUES ('smith', 'smithPwd', 'smith@gmail.com') INSERT INTO @myTable(UserName, Password, Email) VALUES ('Brandy', 'BrandyPwd', 'tom@gmail.com') -- If @sortType = 1 then sort the selected field in ASC ord...

Liskov substitution principle with simple example

Liskov Substitution Principle Definition 2 : Functions that use references to base classes must be able to use objects of derived classes without knowing it. Definition 2 : Object inheriting from base class or interface or other abstraction must be semantically substitutable for the original abstraction. Definition 3 : If a program module is using a Base Class, then the reference to the base class can be replaced with a derived class without affecting the functionality of the program module. Problem We all know that square is a rectangle from geometry. Now create a Rectangle base class with associated Height and Width properties and create Area() method to calculate the area in Rectangle base class.     public class Rectangle     {         public virtual int Height { get ; set ; }         public virtual int Width { get ; set ; }   ...