阅读以下说明和Java代码,回答问题[说明]在某些系统中,存在非常复杂的对象,可以采用循序渐进的方式进行组合将小对象组合,成复杂的对象。以下实例展示了Builder(生成器)模式。该实例用来建立“文件”,文件内容包括:一个标题、一串字符以及一些有项目符号的项目。Builder类规定组成文件的方法,Director类利用这个方法产生一份具体的文件。图6-1显示了各个类间的关系。以下是Java语言实现,能够正确编译通过。[Java代码]//Builder. java文件public (1) class Builder {public abstract void makeTitle(String title);public abstract void makeString(String str);public abstract void makeItems(String[] items);public abstract Object getResult();}//Director. java文件public class Director{private (2) builder;public Director(Builder builder){this. builder = builder;}public Object construct(){builder.makeTitle("Greeting");builder.makeString("从早上到白天结束");builder.makeItems(new String[]{"早安", "午安",});builder.makeString("到了晚上");builder.makeItems(new String[]("晚安", "好梦",});return builder.getResult();}}//TextBuilder.java文件public class TextBuilder (3) Builder{private StringBuffer buffer = new StringBuffer();public void makeTitle(String title){buffer.append("『" + title + "』"\n\n");}public void makeString(String str){buffer.append('■' + str + "\n\n ");}public void makeItems(String[] items){for(int i = 0; i (4) ; i++){buffer.append('·' + items[i] + "\n");}buffer.append("\n");}public Object getResult(){return buffer.toString();}}//Main.java文件public class Main {public static void main(String[] args) {Director director = new Director(new TextBuilder());String result = (String)director. (5) ;System.out.println(result);
阅读以下说明和Java代码,回答问题
[说明]
在某些系统中,存在非常复杂的对象,可以采用循序渐进的方式进行组合将小对象组合,成复杂的对象。
以下实例展示了Builder(生成器)模式。该实例用来建立“文件”,文件内容包括:一个标题、一串字符以及一些有项目符号的项目。Builder类规定组成文件的方法,Director类利用这个方法产生一份具体的文件。图6-1显示了各个类间的关系。
以下是Java语言实现,能够正确编译通过。
[Java代码]
//Builder. java文件
public (1) class Builder {
public abstract void makeTitle(String title);
public abstract void makeString(String str);
public abstract void makeItems(String[] items);
public abstract Object getResult();
}
//Director. java文件
public class Director{
private (2) builder;
public Director(Builder builder){
this. builder = builder;
}
public Object construct(){
builder.makeTitle("Greeting");
builder.makeString("从早上到白天结束");
builder.makeItems(new String[]{"早安", "午安",});
builder.makeString("到了晚上");
builder.makeItems(new String[]("晚安", "好梦",});
return builder.getResult();
}
}
//TextBuilder.java文件
public class TextBuilder (3) Builder{
private StringBuffer buffer = new StringBuffer();
public void makeTitle(String title){
buffer.append("『" + title + "』"\n\n");
}
public void makeString(String str){
buffer.append('■' + str + "\n\n ");
}
public void makeItems(String[] items){
for(int i = 0; i< (4) ; i++){
buffer.append('·' + items[i] + "\n");
}
buffer.append("\n");
}
public Object getResult(){
return buffer.toString();
}
}
//Main.java文件
public class Main {
public static void main(String[] args) {
Director director = new Director(new TextBuilder());
String result = (String)director. (5) ;
System.out.println(result);