Back to list
Views:   0
Replies:  0
Archived
,
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     Licensing       EULA       Sitemap      
© Data & Object Factory, LLC.
Made with    in Austin, Texas