Home  /  Questions  /  Question



275   99.7
Apr 14, 2011


Code review: is this code a proper Factory Pattern?

Hi I am desiging one small project in .NET in that I want to implemet a Factory Pattern to create a instance of a manager at one place, Please review my code attached and suggest if there is a better way.

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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            IManager _manager = new ManagerFactory();
            _manager.StundentManager.SaveData();
            _manager.FeeManager.SaveFee();
            Console.ReadLine();
        }
    }

    interface IStudentManager
    {
        void SaveData();
        int GetStudentID(string studentName);
    }

    interface IFeeManager
    {
        void SaveFee();
    }

    class StudentManager : IStudentManager
    {
        #region IStudentManager Members

        public void SaveData()
        {
            Console.WriteLine("Data Saved");
        }

        public int GetStudentID(string studentName)
        {
            return 0;
        }

        #endregion
    }

    class FeeManager : IFeeManager
    {
        #region IFeeManager Members

        public void SaveFee()
        {
            Console.WriteLine("Fee Saved");
        }

        #endregion
    }


    interface IManager
    {
        IStudentManager StundentManager { get; }
        IFeeManager FeeManager { get; }
    }

    class ManagerFactory : IManager
    {

        #region IManager Members

        public IStudentManager StundentManager
        {
            get { return new StudentManager(); }
        }

        #endregion

        #region IManager Members


        public IFeeManager FeeManager
        {
            get { return new FeeManager(); }
        }

        #endregion
    }
}

 



1,128   99.9
Apr 14, 2011
Not being aware of the domain problem at hand, here's a stab at the code...

To begin with, do you intend to have only one implementation of IStudentManager and one of IFeeManager? If yes, then implementing a factory pattern seems like overkill to me.

In any case, I've rewritten the code a bit with a factory using generics to simplify it a bit. Also focusing only on the IStudentManager interface... the principle is the same for IFeeManager. Using this kind of factory makes it easier to add new implementations of IStudentManager, since you do not need to change it.

Be aware, thought, that there are multiple ways in which you can implement a factory pattern. This is just one of them.


Hope that helps.




public class DoFactory {
        public static void Work() {
            ManagerFactory.CreateStudenManager<BachelorStudentManager>().SaveData();
            ManagerFactory.CreateStudenManager<MasterStudentManager>().SaveData();
        }

        private interface IStudentManager {
            void SaveData();
            int GetStudentId(string studentName);
        }

        private class BachelorStudentManager : IStudentManager {
            public void SaveData() {
                Console.WriteLine("Saving Bachelor Student Data");
            }
            public int GetStudentId(string studentName) {
                return 0;
            }

        }

        private class MasterStudentManager : IStudentManager {
            public void SaveData() {
                Console.WriteLine("Saving Master Student Data");
            }
            public int GetStudentId(string studentName) {
                return 1;
            }
        }

        private static class ManagerFactory {
            public static IStudentManager CreateStudenManager <T>() {
                return (IStudentManager) Activator.CreateInstance(typeof(T));
            }
        }
    }

 
 2 comments
 
Thanks Robert I got your point.. but what if I want to implement the different fuctionality in each manager. I mean In studentManager I may do some basic DML operation but in Fee Manager I need more functions to handle transactions.. so what u suggest for this to handle in best way.. --- Imran Khan  Apr 15, 2011
 
What is the purpose of the FeeManager? If it is to calculate Fees for students, may I suggest that you instead add a new method to the IStudentManager called CalculateFee() ? --- Robert Blixt  Apr 16, 2011