现欲开发一个软件系统,要求能够同时支持多种不同的数据库,为此采用抽象工厂模式设计该系统。以SQL Server和Access两种数据库以及系统中的数据库表Department为例,其类图如图5-1所示。
图5-1类图
【C++代码】
#include<iostream>
using namespace std;
class Department{/*代码省略*/};
class IDepartment{
public:
(1)=0;
(2)=0;
};
class SqlserverDepartment:(3){
public:
void Insert(Department*department){
cout<<"Insert a record into Department in SQL Server!\n";
∥其余代码省略
}
Department GetDepartment(int id){
/*代码省略*/
}
};
class AccessDepartment:(4){
public:
void Insert(Department*department){
cout<<"Insert a record into Department in ACCESS!\n";
∥其余代码省略
}
Department GetDepartment(int id){
/*代码省略*/
}
};
(5){
public:
(6)=0;
};
class SqlServerFactory:public IFactory{
public:
IDepartment*CreateDepartment( ){return new SqlserverDepartment( );}
∥其余代码省略
};
class AccessFactory:public IFactory{
public:
IDepartment*CreateDepartment( ){return new AccessDepartment( );}
∥其余代码省略
};
正确答案及解析
正确答案
解析
(1)virtual void Insert(Departmet*department)
(2)virlual Department GetDepartment(int id)
(3)public IDepartment
(4)public IDepartmcnt
(5)class Ifactory
(6)virtual IDcpartment*CreateDepartment()
本题考查基本面向对象设计模式的运用能力。
抽象工厂设计模式主要是提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。从题目给出的类图可知SqlserverDepartment和AccessDepartment继承于Idepartment。而从第(1)和第(2)空处的程序语句可以知道,这里是定义纯虚函数,而类Idepartment一个抽象类,而在这里需要定义一个什么样的纯虚函数,就需要根据SqlserverDepartment和AccessDepartment类的内容来了解。在这两个类里面都有Insert和GetDepartment这两个函数,因此在Idepartment类中定义的纯虚函数就是这两个函数,因此第(1)空应该填virtual void Insert(Departmet*department),而第(2)空应该填virlual Department GetDepartment(int id)。
第(3)空和第(4)空是一样的,因为类SqlserverDepartment和AccessDepartment都是继承抽象类Idepartment,而一般情况下的继承方式都是public,所以这两空的答案都是public Idepartment。
从第(5)空出现的位置,不难知道这里是定义一个类,结合前后程序,可以知道这里定义的类是Ifactory,这是一个抽象类,因此该空的答案为class Ifactory。
第(6)空是定义抽象类Ifactory的纯虚函数,从后面的程序可以看出,需要定义的纯虚函数是CreateDepartment,因此第(6)空的答案是virtual IDcpartment*CreateDepartment()。
包含此试题的试卷
你可能感兴趣的试题
Advancements in ( )have contributed to the growth of the automotive industry through the creation and evolution of self-driving vehicles.
-
- A.Artificial Intelligence
- B.Cloud Computing
- C.Internet of Things
- D.Big Data
- 查看答案
In project human resource management , ( )is not a source of power for the project manager.
-
- A.referent power
- B.expert power
- C.reward power
- D.audit power
- 查看答案
At the project establishment stage , the feasibility study mainly includes techinical feasibility analysis , ( ), operation environment feasibility analysis and other aspects of feasibility analysis.
-
- A.detail feasibility analysis
- B.opportunity analysis
- C.economic feasibility analysis
- D.risk analysis
- 查看答案
( )is a grid that shows the project resources assigned to each work package.
-
- A.Stakeholder engagement assessment matrix
- B.Requirements traceability matrix
- C.Probability and impact matrix
- D.Responsibility assignment matrix
- 查看答案
Xinhua News Agency reported in January 2022,Chian will further promote the developmet of a digital economy during the 14th Five-Year Plan eriod(2021-2025). The plan also emphasized industrial ( )transformation.
-
- A.digital
- B.networking
- C.intelligentize
- D.informatization
- 查看答案