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),会造成客户程序和对象创建工作的紧耦合.对此,抽象工厂模 ...
随机推荐
- 在ASP.MVC中使用Ajax
Asp.net MVC 抛弃了Asp.net WebForm那种高度封装的控件,让我们跟底层的HTML有了更多的亲近.可以更自由.更灵活的去控制HTML的结构.样式和行为.Asp.net MVC可以更 ...
- java 的 AccessController.doPrivileged使用
AccessController.doPrivileged意思是这个是特别的,不用做权限检查. 在什么地方会用到呢:加入1.jar中有类可以读取一个文件,现在我们要使用1.jar去做这个事情.但是我们 ...
- Oracle nvl(),nvl2()函数介绍
NVL函数 Oracle/PLSQL中的一个函数. 格式为: NVL( string1, replace_with) 功能:如果string1为NULL,则NVL函数返回replace_with的值, ...
- 如何在html中做圆角矩形和 只有右边的"分隔线"
这个网站满好的,可以常看看 css-matic中有几个很好的写css可视化的工具 其实做css 版式布局等都可以有工具的 推荐40个优秀的免费CSS工具 debugger正则表达式在线 其实是对(理论 ...
- CF459D Pashmak and Parmida's problem (树状数组)
Codeforces Round #261 (Div. 2) 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a ...
- Emacs配置文件
;;tab and space;;when true,emacs use mixture of tab and space to archieve(setq-default indent-tabs-m ...
- EasyUI datagrid优化
easyui datagrid 在IE上加载速度慢, 150行数据就无法忍受了. firefox加载速度还可以. jquery easyui datagrid使用参考 http://www.cnblo ...
- 欧几里得证明$\sqrt{2}$是无理数
选自<费马大定理:一个困惑了世间智者358年的谜>,有少许改动. 原译者:薛密 \(\sqrt{2}\)是无理数,即不能写成一个分数.欧几里得以反证法证明此结论.第一步是假定相反的事实是真 ...
- Hanoi问题
#include<stdio.h>int main(){ int m; void hanoi(int n,char x,char y,char z); printf("input ...
- 【bzoj1036】[ZJOI2008]树的统计Count
题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v ...