• AbstractFactory 模式


    //问题描述:操作数据库。数据库可能是Access、SQL等。以操作用户信息、部门信息为例。
    //可能在任意数据库中操作。
    //User类;Department类; IUser,UserOperAccess,UserOperSQL;
    //IDep,DepOperAccess,DepOperSQL;
    //DataAccess:创建出操作类型
    //通过配置文件来管理(通过反射机制动态加载)要创建的操作类型。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    using System.Configuration;

    namespace AbstractFactory
    {
        public class User                //user info
        {
            private string name;
            private int id;

            public string Name
            {
                get { return name; }
                set { name = value; }
            }
            public int Id
            {
                get { return id; }
                set { id = value; }
            }
        }
        public class Department          //department info
        {
            private string depName;
            private int num;

            public string DepName
            {
                get { return depName; }
                set { depName = value; }
            }
            public int Num
            {
                get { return num; }
                set { num = value; }
            }
        }

        public interface IUserOper      //user operation
        {
            void Insert(User user);
            void GetName(int id);
        }
        public class UserOperSQL : IUserOper
        {
            public void Insert(User user)
            {
                Console.WriteLine("Insert  user info into SQL ");
            }
            public void GetName(int id)
            {
                Console.WriteLine("Get user info from SQL");
            }
        }
        public class UserOperAccess : IUserOper
        {
            public void Insert(User user)
            {
                Console.WriteLine("Insert user info into Access");
            }
            public void GetName(int id)
            {
                Console.WriteLine("Get user info from Access");
            }
        }
        public interface IDepartOper   //department operation
        {
            void Insert(Department dep);
            void GetName(int num);
        }
        public class DepOperSQL : IDepartOper
        {
            public void Insert(Department dep)
            {
                Console.WriteLine("Insert department info into SQL ");
            }
            public void GetName(int num)
            {
                Console.WriteLine("Get department info from SQL ");
            }
        }
        public class DepOperAccess : IDepartOper
        {
            public void Insert(Department dep)
            {
                Console.WriteLine("Insert department info into Access ");
            }
            public void GetName(int num)
            {
                Console.WriteLine("Get department info from Access ");
            }
        }
        public class DataAccess
        {
            public static readonly string AssemblyName = "AbstractFactory";
            public static readonly string dbName = ConfigurationManager.AppSettings["DB"];

            public static IUserOper CreateUserOper()       //创建用户操作:  Access、 SQL 等
            {
                string userName = AssemblyName + "." + "UserOper" + dbName;
                return (IUserOper)Assembly.Load(AssemblyName).CreateInstance(userName);
            }
            public static IDepartOper CreateDepOper()      //创建部门操作:  Access、 SQL 等
            {
                string depName = AssemblyName + "." + "DepOper" + dbName;
                return (IDepartOper)Assembly.Load(AssemblyName).CreateInstance(depName);
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                User u1 = new User();
                Department d1 = new Department();

                IUserOper iu = DataAccess.CreateUserOper();
                iu.Insert(u1);
                iu.GetName(1);

                IDepartOper id = DataAccess.CreateDepOper();
                id.Insert(d1);
                id.GetName(1);

                Console.Read();
            }
        }
    }
    //配置文件内容:App.config
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="DB" value="SQL"/>         //可更换
      </appSettings>
    </configuration>

  • 相关阅读:
    关于最短路算法
    牛客网练习赛7-D-无向图(bfs,链式前向星)
    51nod蜥蜴与地下室(1498)(暴力搜索)
    poj1062昂贵的聘礼(枚举+最短路)
    训练题(代码未检验)(序列前k大和问题)
    两个序列求前k大和
    欧拉回路
    hdu 6063 RXD and math
    hdu 6066 RXD's date
    bzoj 4300 绝世好题
  • 原文地址:https://www.cnblogs.com/MayGarden/p/1529072.html
Copyright © 2020-2023  润新知