基本概念:
- 构建者模式的定义
将构建的过程和表示过程进行分离
构建的过程:隐藏
表示过程 : 开放 - 构建者模式的场景
创建一个复杂的对象,同时改复杂对象有很多默认值(初始化)的时候,可以使用构建者模式。(典型:给对象设置可选参数) - 构建者模式案例
AlretDialog - 构建者模式-UML类图结构
4.1 分析角色划分
角色一:产品
角色二:抽象构建者
角色三:具体构建者
角色四:组装
public class ComListDialog extends Dialog {
public static Context mContext;
private TextView btnCancel;
private LinearLayout lyContents;
private static List<CommonListMenuItem> btnMenu;
public ComListDialog(@NonNull Context context) {
super(context, R.style.common_dialog);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window win = getWindow();
win.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = win.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
win.setWindowAnimations(R.style.listDialogWindowAnim);
win.setAttributes(lp);
setContentView(R.layout.common_list_dialog);
setCanceledOnTouchOutside(true);
setCancelable(true);
findViewById(R.id.menu_base_content).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
btnCancel = (TextView) findViewById(R.id.menu_cancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
lyContents = (LinearLayout) findViewById(R.id.menu_content);
if (btnMenu != null && btnMenu.size() > 0) {
for (int i = 0; i < btnMenu.size(); i++) {
View v = View.inflate(mContext, R.layout.common_list_dialog_item, null);
if (i == 0) {
v.findViewById(R.id.menu_line).setVisibility(View.GONE);
}
TextView tv = (TextView) v.findViewById(R.id.menu_button);
tv.setText(btnMenu.get(i).getName());
tv.setTextColor(btnMenu.get(i).color);
tv.setPadding(20, 0, 20, 0);
tv.setGravity(Gravity.CENTER);
if (btnMenu.get(i).textsize != 0) {
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, btnMenu.get(i).textsize);
}
tv.setOnClickListener(btnMenu.get(i).getClickListener());
lyContents.addView(v);
}
}
}
// 类似于Javabean
public static class CommonListMenuItem {
private String name;
private View.OnClickListener clickListener;
private int color = Color.parseColor("#209E89");
private int textsize = 0;
public CommonListMenuItem(String name, View.OnClickListener clickListener, int color) {
this.name = name;
this.clickListener = clickListener;
this.color = color;
}
public CommonListMenuItem(String name, View.OnClickListener clickListener, int color, int textsize) {
this.name = name;
this.clickListener = clickListener;
this.color = color;
this.textsize = textsize;
}
public CommonListMenuItem(String name, int color, int textsize) {
this.name = name;
this.color = color;
this.textsize = textsize;
}
public CommonListMenuItem(String name, View.OnClickListener clickListener) {
this.name = name;
this.clickListener = clickListener;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public View.OnClickListener getClickListener() {
return clickListener;
}
public void setClickListener(View.OnClickListener clickListener) {
this.clickListener = clickListener;
}
}
//构建
public static class Builder {
public Builder(Context context) {
mContext = context;
btnMenu = new ArrayList<CommonListMenuItem>();
}
public Builder addMenuItem(CommonListMenuItem item) {
btnMenu.add(item);
return this;
}
public ComListDialog show() {
if (null != mContext && (!(mContext instanceof Activity) || !((Activity) mContext).isFinishing())) {
ComListDialog dialog = new ComListDialog(mContext);
dialog.show();
return dialog;
}
return null;
}
}
@Override
public void dismiss() {
super.dismiss();
btnMenu = null;
mContext = null;
}
}
5F54B8A8-BB9A-4123-95F4-F6C3E9400AB7.png
网友评论