题目详情

生成器(Builder)模式的意图是将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。图5-1所示为其类图。

中级软件设计师,历年真题,2018年上半年(下午)《软件设计师》真题

【C++代码】

#include<iostream>

#include<string>

using namespace std;

class Product{

private:

string partA,partB;

public:

Product(  ){}

void setPartA(const string&s){PartA=s;}

void setPartB(const string&s){PartB=s;}

//其余代码省略

};

class Builder{

public:

(1);

virtual void buildPartB(  )=0;

(2);

};

class ConcreteBuilder1:public Builder{

private:

Product*product;

public:

ConcreteBuilder1(  ){product=new Product(  );}

void buildPartA(  ){(3)("Component A");}

void buildPartB(  ){(4)("Component B");}

Product*getResult(  ){return product;}

//其余代码省略

};

class ConcreteBuilder2:public Builder{

/*代码省略*/

};

class Director{

private:

Builder*builder;

public:

Director(Builder*pBuilder){builder=pBuilder;}

void construct(  ){

(5);

//其余代码省略

}

//其余代码省略

};

int main(  ){

Director*director1=new Director(new ConcreteBuilder1(  ));

director1->construct(  );

delete director1;

return 0;

}

正确答案及解析

正确答案
解析

(1)virtual void buildPartA()=0

(2)virtual Product*getResult()=0

(3)product->setPartA

(4)product->setPartB

(5)builder->buildPartA();

或builder->buildPartB();

本题考查的是面向对象程序设计,是JAVA语言与设计模式的结合考查。本题涉及的设计模式是构建器模式,将复杂类的构造过程推迟到子类实现。

对于第一空、第二空,根据实现接口的类,补充其接口缺失的方法,因此,空(1)和空(2)分别填写:virtual void buildPartA()=0和virtual Product*getResult()=0,二者可以互换;

对于第三空、第四空,是根据product类方法进行的补充,与具体产品的实现保持一致,因此,分别填写:product->setPartA,product->setPartB;

对于第五空,由于在填空后面跟随的是代码省略,因此题目并不严谨,缺失的语句可以有builder->buildPartA();builder->buildPartB()。

包含此试题的试卷

你可能感兴趣的试题

单选题

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
查看答案

相关题库更多 +