设计模式之-抽像工厂

 class User
    {
        private int _id;

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }

    interface IUser
    {
        User GetUser(int id);
        void Insert(User user);
    }

    class SqlserverUser : IUser
    {
        public void Insert(User user)
        {
            Console.WriteLine("在Sql Server中给User表增加一条记录 ");
        }

        public User GetUser(int id)
        {
            Console.WriteLine("在Sql Server中根据ID得到User表中一条 记录");
            return null;
        }
    }
    class AccessUser : IUser
    {
        public void Insert(User user)
        {
            Console.WriteLine("在Access中给User表增加一条记录 ");
        }

        public User GetUser(int id)
        {
            Console.WriteLine("在Access中根据ID得到User表中一条 记录");
            return null;
        }
    }

    class DataAccess
    {
        private static readonly string AssemblyName = "程序集名称";
        private static readonly string db = "Sqlserver";  //读配置文件

        public static IUser CreateUser()
        {
            string className = AssemblyName + "." + db + "User";
            IUser _user = (IUser)Assembly.Load(AssemblyName).CreateInstance(className);
            return _user;
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            User user = new User();

            IUser su = DataAccess.CreateUser();

            su.Insert(user);
            su.GetUser(1);

            Console.ReadKey();
        }
    }

 

posted @ 2013-05-04 00:45  lhfly  阅读(212)  评论(0编辑  收藏  举报