5.现欲开发一个软件系统,要求能够同时支持多种不同的数据库,为此采用抽象工厂模式设计该系统。以SQL Server和Access两种数据库以及系统中的数据库表Department为例,其类图如图6-1所示。
图6-1类图
【Java代码】
import javA.util.*;
class Department{/*代码省略*/}
interface IDepartment{
(1);
(2);
}
class SqlserverDepartment(3){
public voidInsert(Department department){
System.out.println(”Insert a record into Department in SQL Server!");
∥其余代码省略
}
public Department GetDepartment(int id){
/*代码省略*/
}
}
classAccessDepartment(4){
public void Insert(Department department){
System.out.println("Insert a record into Department in ACCESS!”);
∥其余代码省略
}
public Department GetDepartment(int id){
/*代码省略*/
}
}
(5){
(6);
}
class SqlServerFactory implements IFactory{
public IDepartment CreateDepartment( ){
retum new SqlserverDepartment( );
}
∥其余代码省略
}
class AccessFactory implements IFactory{
public IDepartment CreateDepartment( ){
return new AccessDepartment( );
}
∥其余代码省略
}