C# 反射+抽象工厂模式
此模式可以很好的更换程序使用不同的数据库
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# 反射+抽象工厂模式的更多相关文章
- 设计模式之抽象工厂模式(附带类似反射功能的实现/c++)
问题描述 假设我们要开发一款游戏, 当然为了吸引更多的人玩, 游戏难度不能太大(让大家都没有信心了,估计游戏也就没有前途了),但是也不能太简单(没有挑战性也不符合玩家的心理).于是我们就可以采用这样一 ...
- C#设计模式之:抽象工厂模式与反射
抽象工厂模式[实例]:定义一个用于创建对象的接口,让子类决定实例化哪一个类 UML 代码class User{ private int _id; public int Id { get = ...
- 抽象工厂模式(JAVA反射)
实例代码(JAVA):模式动机 在工厂方法模式中具体工厂负责生产具体的产品,每一个具体工厂对应一种具体产品,工厂方法也具有唯一性,一般情况下,一个具体工厂中只有一个工厂方法或者一组重载的工厂方 ...
- Net设计模式实例之抽象工厂模式(Abstract Factory Pattern)
一.抽象工厂模式简介(Bref Introduction) 抽象工厂模式(Abstract Factory Pattern),提供一个创建一系列相关或者相互依赖对象的接口,而无需制定他们的具体类.优点 ...
- 反射 + 抽象工厂模式切换DB数据源(附Demo)
首先,设计模式的文章源自于程杰的<大话设计模式>这本书,这本书个人感觉很适合我,看着不累,能够安安心心的阅读学习.在这里十分感谢程杰的这本书,我博文中的例子会根据书上的例子来.为了不侵犯这 ...
- 设计模式(3)--抽象工厂模式(Absrtact Factory Pattern)
定义 抽象工厂模式的实质就是提供接口来创建一系列相关或独立的对象而不指定这些对象的具体类. 理解 在软件系统中,经常面临着"一系列相互依赖的对象"的创建工作:同时由于需求的变化,往 ...
- 大话设计模式C++版——抽象工厂模式
前面说过,简单工厂模式是最基础的一种设计模式,那以工厂命名的设计模式就是23种设计模式中最多的一种,他们一脉相承,一步一步进化而来,这里就是其中的最后一种——抽象工厂模式(Abstract Facto ...
- 深入浅出设计模式——抽象工厂模式(Abstract Factory)
模式动机在工厂方法模式中具体工厂负责生产具体的产品,每一个具体工厂对应一种具体产品,工厂方法也具有唯一性,一般情况下,一个具体工厂中只有一个工厂方法或者一组重载的工厂方法.但是有时候我们需要一个工厂可 ...
- C#设计模式——抽象工厂模式(Abstract Factory Pattern)
一.概述在软件开发中,常常会需要创建一系列相互依赖的对象,同时,由于需求的变化,往往存在较多系列对象的创建工作.如果采用常规的创建方法(new),会造成客户程序和对象创建工作的紧耦合.对此,抽象工厂模 ...
随机推荐
- 关于http请求
response的Head值: 200 : 请求已成功,请求所希望的响应头或数据体将随此响应返回 304 : Not Modified 客户端有缓冲的文档并发出了一个条件性的请求,原来缓冲的文档还 ...
- ngrok内网穿透利器在windws下的使用
1.到官网下载windows版本:https://ngrok.com/download 2.解压,双击“ngrok.exe” 3.输入“ngrok http 80”,会随机给你分配域名.见下图. ng ...
- 使用/调用 函数的时候, 前面加不加 对象或 this?
这个问题, 其实没有细想: 应该是这样的: (想明白了, 就会少很多困惑, 会对语言的把握 会 更深入更透彻) 任何一门 语言, (如果你自己去设计一门语言...). 都要规定 一些 "关键 ...
- xinetd
最简安装centos6.4时,xinetd服务是没有安装的,只是在/etc下有xinetd.d目录, 没有xinetd.conf这个配置文件 xinetd is a secure replacemen ...
- 深入理解计算机系统-从书中看到了异或交换ab两个值的新感
还得从一个很经典的面试题说起:不通过第三个变量来交换两个变量a,b的值... 一个很经典的答案是通过异或来解决: 第壹步:a=a^b; 第贰步:b=a^b; 第叁步:a=a^b; 以前提起" ...
- PHP基础 之 基本数据类型练习
<h3>PHP基础练习</h3> <?php echo "<h4>常量</h4>"; //定义:一般大写,使用下划线间隔 de ...
- linux下svn命令使用大全
最近经常使用svn进行代码管理,这些命令老是记不住,得经常上网查,终于找了一个linux下svn命令使用大全:1.将文件checkout到本地目录 svn checkout path(path是服务器 ...
- 几个Jquery对话框插件
项目现状 While Thickbox had its day, it is not maintained any longer, so we recommend you use some alter ...
- SSH如何通过公钥连接云服务器
导读 通常我们连接远程服务器(linux)windows下通过putty或xshell等工具远程连接.linux下可以直接通过ssh命令连接.其实这两者都是一致的,都是通过ssh协议进行传输. 如果我 ...
- NSTimer用法,暂停,继续,初始化
NSTimer用法,暂停,继续,初始化 转载:http://blog.csdn.net/zhuzhihai1988/article/details/7742881 NSTimer的使用方法 1.初始化 ...