Dofactory.com
Dofactory.com
 Back to list
Views:   8.6K
Replies:  1
Archived

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
    }
}

 
Imran Khan, Apr 14, 2011
Reply 1
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));
            }
        }
    }

 
Robert Blixt, Apr 14, 2011
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.