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

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. aop实现日志(转)

    文章来至于http://www.thinksaas.cn/group/topic/341436/ 第一:>>在spring的配置文件里增加以下配置 <!-- 支持 @AspectJ ...

  2. linux访问windows共享文件夹的方法

    博客转自:http://www.01happy.com/linux-access-windows-shares-folders/ 有时需要在linux下需要访问windows的共享文件夹,可以使用mo ...

  3. connect() failed (111: Connection refused) while connecting to upstream

    配置好lamp后,在浏览器中运行程序后,出现上面的错误. 转自:http://www.xuejiehome.com/blread-1828.html I'm experiencing 502 gate ...

  4. Tomcat6.0 管理器配置

    最近忙着毕业答辩,填写材料,好多事情都给耽搁了!一个月都没有继续翻译tomcat,这回有点时间赶紧补上. 这部分,其实对开发者或者tomcat管理者来说,只要会登录页面管理器或者使用写简单的http就 ...

  5. DatePicker隐藏年月日

    1.隐藏年 ((ViewGroup) (((ViewGroup) dp.getChildAt(0)).getChildAt(0))) .getChildAt(0).setVisibility(View ...

  6. Ruby类的继承

    Ruby继承的语法 class DerivedClass < BaseClass #some stuff end < 为继承符号 重写(override) 的概念 有时, 我们希望子类从父 ...

  7. SQL2008"阻止保存要求重新创建表的更改"问题的解决

    在sql server2008中修改数据库中某个字段的时候,会弹出以下提示: 导致数据库表在表设计界面无法修改,好像只能通过sql语句修改,其实只要改一下sql server的一个配置项就可以了,具体 ...

  8. MySQL性能优化的21条最佳经验【转】

    转载自http://www.cnblogs.com/jiaosq/p/5843437.html 今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只 ...

  9. Android内存性能优化(内部资料总结) eoe转载

    刚入门的童鞋肯能都会有一个疑问,Java不是有虚拟机了么,内存会自动化管理,我们就不必要手动的释放资源了,反正系统会给我们完成.其实Java中没有指针的概念,但是指针的使用方式依然存在,一味的依赖系统 ...

  10. [KOJ0574NOIP200406合并果子]

    [COJ0574NOIP200406合并果子] 试题描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆.    每一次合并,多多可以把两 ...