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),会造成客户程序和对象创建工作的紧耦合.对此,抽象工厂模 ...
随机推荐
- java 获取文件的最后编辑时间
还是日志的问题,需要把日志文件的一些信息给显示出来,其中就需要显示最后的编辑时间,在网上找的答案... File f = new File(path); SimpleDateFormat sdf = ...
- hibernate 批量增加 修改 删除
4.2 Hibernate的批量处理 Hibernate完全以面向对象的方式来操作数据库,当程序里以面向对象的方式操作持久化对象时,将被自动转换为对数据库的操作.例如调用Session的delete ...
- js调用ios的方法
摘要 在做h5应用的时,有时有些功能js并不能实现的特别完美.比如下载进度条或上传文件进度等.如果能调用ios或者android的方法,实现进度,以及文件上传或者下载列表更好一些.如果使用第三方的js ...
- JQuery中的html(),text(),val()区别
jQuery中.html()用为读取和修改元素的HTML标签,.text()用来读取或修改元素的纯文本内容,.val()用来读取或修改表单元素的value值. 1.HTML html():取得第一个匹 ...
- HDOJ 3593 The most powerful force
树形DP / 泛化物品的背包...可以去看09年徐持衡论文<浅谈几类背包问题> The most powerful force Time Limit: 16000/8000 MS (Jav ...
- Linux 开机启动方式设置 inittab 详解,开机直接进入“命令行”模式
Linux下的 /etc/inittab 中的英文解释: This file describes how the INIT process should set up the system in a ...
- 浮动层-JS兼容IE6
//引用jquery 包/*orderBycat 与 orderBycatHead 双层浮动*/ $(window).scroll(function () { var top = $(window). ...
- 类加载器ClassLoader之jar包隔离
小引子 最近做了一个根据同一模块的不同jar版本做同时测试的工具,感觉挺有意思,特此记录. 类加载器(ClassLoader)是啥? 把类加载阶段中的"通过一个类的全限定名(博主注:绝对路径 ...
- HDU 1532 最大流模板题
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1532 最近在学网络流,学的还不好,先不写理解了,先放模板... 我觉得写得不错的博客:http://blo ...
- lintcode 447 Search in a Big Sorted Array
Given a big sorted array with positive integers sorted by ascending order. The array is so big so th ...