实现从Access换为使用Sqlserver,接着换为Oracle
源代码:
class User
{
private int ID;
private String name;
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class SqlserverUser
{
public void Insert(User user)
{
System.out.println("在User表中增加一条记录");
}
public User GetUser(int id)
{
System.out.println("根据ID得到User表中的一条记录");
return null;
}
}
public class main {
public static void main(String[] args) {
User user=new User();
SqlserverUser su=new SqlserverUser();//数据库固定
su.Insert(user);
su.GetUser(1);
}
}
使用工厂模式方法:
class User
{
private int ID;
private String name;
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
interface IUser
{
void Insert(User user);
User GetUser(int id);
}
class SqlserverUser implements IUser
{
public void Insert(User user)
{
System.out.println("在User表中增加一条记录");
}
public User GetUser(int id)
{
System.out.println("根据ID得到User表中的一条记录");
return null;
}
}
class AccessUser implements IUser
{
public void Insert(User user)
{
System.out.println("在User表中增加一条记录");
}
public User GetUser(int id)
{
System.out.println("根据ID得到User表中的一条记录");
return null;
}
}
interface IFactory
{
IUser createUser();
}//定义一个创建访问User表对象的工厂接口
class SqlserverFactory implements IFactory
{
@Override
public IUser createUser() {
// TODO Auto-generated method stub
return new SqlserverUser();
}
}
class AccessFactory implements IFactory
{
@Override
public IUser createUser() {
// TODO Auto-generated method stub
return new AccessUser();
}
}//具体工厂类
public class main {
public static void main(String[] args) {
User user=new User();
IFactory su=new SqlserverFactory();//若要修改Access数据库,只须修改此处
IUser iu=su.createUser();
iu.Insert(user);
iu.GetUser(1);
}
}
若增加一个新的表如部门表(Department表)
就要增加抽象工厂类
class User
{
private int ID;
private String name;
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Department
{
private int id;
private String deptname;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDeptname() {
return deptname;
}
public void setDeptname(String deptname) {
this.deptname = deptname;
}
}
interface IUser
{
void Insert(User user);
User GetUser(int id);
}
interface IDepartment
{
void Insert(Department department);
Department GetDepartment(int id);
}
interface IFactory//IFactory接口,增加一个创建访问Department表对象的抽象接口工厂
{
IUser CreateUser();
IDepartment CreateDepartment();
}
class SqlserverUser implements IUser
{
public void Insert(User user)
{
System.out.println("在User表中增加一条记录");
}
public User GetUser(int id)
{
System.out.println("根据ID得到User表中的一条记录");
return null;
}
}
class SqlserverDepartment implements IDepartment
{
@Override
public void Insert(Department department) {
// TODO Auto-generated method stub
System.out.println("在department表中增加一条记录");
}
@Override
public Department GetDepartment(int id) {
// TODO Auto-generated method stub
System.out.println("根据ID得到department表中的一条记录");
return null;
}
}
class SqlserverFactory implements IFactory
{
@Override
public IUser CreateUser() {
// TODO Auto-generated method stub
return new SqlserverUser();
}
@Override
public IDepartment CreateDepartment() {
// TODO Auto-generated method stub
return new SqlserverDepartment();
}
}
class AccessUser implements IUser
{
public void Insert(User user)
{
System.out.println("在User表中增加一条记录");
}
public User GetUser(int id)
{
System.out.println("根据ID得到User表中的一条记录");
return null;
}
}
class AccessDepartment implements IDepartment
{
@Override
public void Insert(Department department) {
// TODO Auto-generated method stub
System.out.println("在department表中增加一条记录");
}
@Override
public Department GetDepartment(int id) {
// TODO Auto-generated method stub
System.out.println("根据ID得到department表中的一条记录");
return null;
}
}
class AccessFactory implements IFactory
{
public IUser CreateUser() {
// TODO Auto-generated method stub
return new AccessUser();
}
@Override
public IDepartment CreateDepartment() {
// TODO Auto-generated method stub
return new AccessDepartment();
}
}
//客户端
public class main {
public static void main(String[] args) {
User user=new User();
IFactory su=new SqlserverFactory();//只需修改此处,实现数据库访问的切换
IUser iu=su.CreateUser();
iu.Insert(user);
iu.GetUser(1);
Department dept=new Department();
IDepartment id=su.CreateDepartment();
id.Insert(dept);
id.GetDepartment(1);
}
}
抽象工厂可以很方便的切换不同数据库访问的代码,但是改动量非常多。
用简单工厂改进抽象工厂,去除IFactory,SqlservserFactory和AccessFactory三个工厂类,用DataAccess类代替
class DataAccess
{
private static String db="Sqlserver";//Access
public static IUser CreatUser()
{
IUser result=null;
switch(db)
{
case "Sqlserver":
result=new SqlserverUser();break;
case "Access":
result=new AccessUser();
break;
}
return result;
}
public static IDepartment CreatDepartment()
{
IDepartment result=null;
switch(db)
{
case "Sqlserver":
result=new SqlserverDepartment();break;
case "Access":
result=new AccessDepartment();
break;
}
return result;
}
}
//客户端已经不需要受改动数据库访问的影响
public class main {
public static void main(String[] args) {
User user=new User();
Department dept=new Department();
IUser iu=DataAccess.CreatUser();//直接得到实例,而不需要存在任何依赖
iu.Insert(user);
iu.GetUser(1);
IDepartment id=DataAccess.CreatDepartment();
id.Insert(dept);
id.GetDepartment(1);
}
//事先设置了db的值所以简单工厂的方法都不需要输入参数
}