classProduct {
private String partA;//定義部件,部件能夠是任意類型,包括值類型和引用類型
private String partB;
private String partC;
}
abstract classBuilder{
//建立產品對象
protected Product product=newProduct();
public abstract void buildPartA();
public abstract void buildPartB();
public abstract void buildPartC();
//返回產品對象
public Product getResult(){
return product;
}
}
classDirector{
private Builder builder;
public Director(Builder builder){
this.builder=builder;
}
//管理控制產品構建與組裝方法
publicProduct construct(){
builder.buildPartA();
builder.buildPartB();
builder.buildPartC();
return builder.getResult();
}
}
……
Builder builder =newConcreteBuilder();//能夠經過配置文件來存儲具體建造者類ConcreteBuilder的類名,使得更換新的建造者時無須修改源代碼,系統擴展更爲方便
Director director =new Director(builder);
Product product = director.construct();
……
protectedvoid dialog(){
AlertDialog.Builder builder =newBuilder(Main.this);
builder.setMessage("確認退出嗎?");
builder.setTitle("提示");
builder.setPositiveButton("確認",newOnClickListener(){
@Override
publicvoid onClick(DialogInterface dialog,int which){
dialog.dismiss();
Main.this.finish();
}
});
builder.setNegativeButton("取消",newOnClickListener(){
@Override
publicvoid onClick(DialogInterface dialog,int which){
dialog.dismiss();
}
});
builder.create().show();
}