此模式可以很好的更换程序使用不同的数据库

1.用到的属性类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ReflectionAndAbstractFactor
{
class User
{ public int Id
{
get;
set;
} public String Name
{
get;
set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ReflectionAndAbstractFactor
{
class Department
{
public int Id
{
get;
set;
} public string DepartmentName
{
get;
set;
} }
}

2.接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ReflectionAndAbstractFactor
{
interface IUser
{ void InsertUser(User user);
User GetUser(int id);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ReflectionAndAbstractFactor
{
interface IDepartment
{
void InsertDepartment(Department department); Department GetDepartment(int id); }
}

3.实现接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ReflectionAndAbstractFactor
{
class SqlServerUser:IUser
{ public void InsertUser(User user)
{ Console.WriteLine("sql server insert user " + user);
} public User GetUser(int id)
{
Console.WriteLine("sql server get user " );
return null;
} }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ReflectionAndAbstractFactor
{
class SqlServerDepartment:IDepartment
{
public void InsertDepartment(Department department)
{
Console.WriteLine("sql server insert department");
} public Department GetDepartment(int id)
{ Console.WriteLine("sql server get department");
return null; } }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ReflectionAndAbstractFactor
{
class OracleUser:IUser
{
public void InsertUser(User user)
{
Console.WriteLine("oracle inser user"); } public User GetUser(int id)
{
Console.WriteLine("oracle get user");
return null;
} }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ReflectionAndAbstractFactor
{
class OracleDepartment:IDepartment
{
public void InsertDepartment(Department department)
{
Console.WriteLine("oracle insert department");
} public Department GetDepartment(int id)
{ Console.WriteLine("oracle get department");
return null;
} }
}

4.反射

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Configuration;
namespace ReflectionAndAbstractFactor
{
class DataAcess
{
private static readonly string assemblyName = "ReflectionAndAbstractFactor";
//private static readonly string db = "SqlServer"; private static readonly string db = ConfigurationManager.AppSettings["db"].ToString();
public static IUser CreateUser()
{
string className = assemblyName + "." + db + "User";
return (IUser)Assembly.Load(assemblyName).CreateInstance(className); } public static IDepartment CreateDepartment()
{
string className = assemblyName + "." + db + "Department";
return (IDepartment)Assembly.Load(assemblyName).CreateInstance(className); } }
}

5.使用

   private void button1_Click(object sender, EventArgs e)
{
User user=new User();
user.Name="user name";
user.Id=;
IUser _User = DataAcess.CreateUser();
_User.InsertUser(user);
_User.GetUser(); Department department=new Department();
department.DepartmentName = "department name";
department.Id = ; IDepartment _Department = DataAcess.CreateDepartment();
_Department.InsertDepartment(department);
_Department.GetDepartment(); }

C# 反射+抽象工厂模式的更多相关文章

  1. 设计模式之抽象工厂模式(附带类似反射功能的实现/c++)

    问题描述 假设我们要开发一款游戏, 当然为了吸引更多的人玩, 游戏难度不能太大(让大家都没有信心了,估计游戏也就没有前途了),但是也不能太简单(没有挑战性也不符合玩家的心理).于是我们就可以采用这样一 ...

  2. C#设计模式之:抽象工厂模式与反射

    抽象工厂模式[实例]:定义一个用于创建对象的接口,让子类决定实例化哪一个类 UML 代码class User{    private int _id;    public int Id { get = ...

  3. 抽象工厂模式(JAVA反射)

    实例代码(JAVA):模式动机     在工厂方法模式中具体工厂负责生产具体的产品,每一个具体工厂对应一种具体产品,工厂方法也具有唯一性,一般情况下,一个具体工厂中只有一个工厂方法或者一组重载的工厂方 ...

  4. Net设计模式实例之抽象工厂模式(Abstract Factory Pattern)

    一.抽象工厂模式简介(Bref Introduction) 抽象工厂模式(Abstract Factory Pattern),提供一个创建一系列相关或者相互依赖对象的接口,而无需制定他们的具体类.优点 ...

  5. 反射 + 抽象工厂模式切换DB数据源(附Demo)

    首先,设计模式的文章源自于程杰的<大话设计模式>这本书,这本书个人感觉很适合我,看着不累,能够安安心心的阅读学习.在这里十分感谢程杰的这本书,我博文中的例子会根据书上的例子来.为了不侵犯这 ...

  6. 设计模式(3)--抽象工厂模式(Absrtact Factory Pattern)

    定义 抽象工厂模式的实质就是提供接口来创建一系列相关或独立的对象而不指定这些对象的具体类. 理解 在软件系统中,经常面临着"一系列相互依赖的对象"的创建工作:同时由于需求的变化,往 ...

  7. 大话设计模式C++版——抽象工厂模式

    前面说过,简单工厂模式是最基础的一种设计模式,那以工厂命名的设计模式就是23种设计模式中最多的一种,他们一脉相承,一步一步进化而来,这里就是其中的最后一种——抽象工厂模式(Abstract Facto ...

  8. 深入浅出设计模式——抽象工厂模式(Abstract Factory)

    模式动机在工厂方法模式中具体工厂负责生产具体的产品,每一个具体工厂对应一种具体产品,工厂方法也具有唯一性,一般情况下,一个具体工厂中只有一个工厂方法或者一组重载的工厂方法.但是有时候我们需要一个工厂可 ...

  9. C#设计模式——抽象工厂模式(Abstract Factory Pattern)

    一.概述在软件开发中,常常会需要创建一系列相互依赖的对象,同时,由于需求的变化,往往存在较多系列对象的创建工作.如果采用常规的创建方法(new),会造成客户程序和对象创建工作的紧耦合.对此,抽象工厂模 ...

随机推荐

  1. swift错误和异常处理

    异常 (exception) 和错误 (error). 在 Objective-C 开发中,异常往往是由程序员的错误导致的 app 无法继续运行,比如我们向一个无法响应某个消息的NSObject 对象 ...

  2. ESI 动态缓存技术

    任何一个Web网站的内容都是在不断更新和变化,但这并不意味这这个网站的内容就是动态内容,事实上,动态的内容是指用户每次点击 相同的链接时取的的内容是由Web服务器应用程序生成的,如常见得ASP,JSP ...

  3. django学习<二>:连接数据库

    发现假如没有很迫切的实际需求或者外界的压力的话,我这种人就很容易偷懒,之前看了一篇比较权威的谈django的文章,里面列举支持的数据库只有四种, 可是我熟悉的数据库只有sqlserver,然后就又怠工 ...

  4. php开发总结

    ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=shared,mys ...

  5. WCF服务显示的是服务器名称而不是IP地址...

    打开http://xx.xx.xx.xx:端口号/Service1.svc页面显示的服务地址为: http://xx_yy_server:端口号/Service1.svc?wsdl 是显示的服务器的名 ...

  6. SQL如何将A,B,C替换为'A','B','C'

    因为涉及到逗号,和单引号' 本来想一次转换成功, 但是最后貌似没有好的办法, 只有分两次完成了 select REPLACE(REPLACE('A,B,C',',','>,>'),'> ...

  7. Codeforces Round #270 1001

    Codeforces Round #270 1001 A. Design Tutorial: Learn from Math time limit per test 1 second memory l ...

  8. 天翼宽带政企网关B2-1P 如何获得超级管理员账号?

    RT 用useradmin没办法做NAT,想进telecomadmin里面看看,,,,,并且已经使用过nE7jA%5m这个密码登录,没有用! 求办法!!! 最佳答案 查找超级管理员密码方法: 1.用光 ...

  9. LoadRunner脚本编写(转)

    性能测试工程师要懂代码么?答案是必须的.好多测试员认为在loadrunner中编写脚本很难很牛X ,主要是大多测试人员并未做过开发工作,大学的那点程序基础也忘记的差不多了.还有非计算机专业出身的测试员 ...

  10. 怎样用路由器共享需要网页认证的wifi

    设置步骤:第一步:登录管理界面 1.连接电脑使用单机能上网的电脑,通过网线连接到路由器的LAN口.2.登录管理界面打开电脑的浏览器,清空地址栏后,输入路由器的管理地址(以路由器底部标贴标识的管理地址为 ...