阅读下列说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
生成器(Builder)模式的意图是将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。图5-1所示为其类图。
【C++代码】#include#includeusingnamespacestd;
classProduct{private:
stringpartA,partB;public:
Product(){}
voidsetPartA(conststring&s){PartA=s;}
voidsetPartB(conststring&s){PartB=s;}
∥其余代码省略
};
classBuilder{public:
(1);
virtualvoidbuildPartB()=0;
(2);
};
classConcreteBuilder1:publicBuilder{private:
Product*product;public:
ConcreteBuilder1(){product=newProduct();}
voidbuildPartA(){(3)("ComponentA");}
voidbuildPartB(){(4)("ComponentB");}
Product*getResult(){returnproduct;}
∥其余代码省略
};
classConcreteBuilder2:publicBuilder{
/*代码省略*/
};
classDirector{private:
Builder*builder;public:
Director(Builder*pBuilder){builder*pBuilder;}
voidconstruct(){
(5)
∥其余代码省略
}
∥其余代码省略
};
intmain(){
Director*director1=newDirector(newConcreteBuilder1());
directorl->construct();
deletedirector1;
return0;
}
参考答案:
(1) virtualvoidbuildPartA()=0
(2) virtualProduct*getResult()=0
(3) product->setPartA
(4) product->setPartB
(5) builder->buildPartA();
builder->buildPartB();
Product*p=builder->getResult();