builder设计模式
来源:互联网 发布:教育软件开发 编辑:程序博客网 时间:2024/06/04 11:01
本文转自:http://blog.csdn.net/bboyfeiyu/article/details/15504187
在Android中经常要使用AlertDialog来显示一些简单的窗口,通常的写法类似下面的代码 :
- AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("提示")
- .setMessage("确定退出?").create();
- dialog.show();
- package com.umeng.dp.builder;
- /**
- * @Copyright: Umeng.com, Ltd. Copyright 2011-2015, All rights reserved
- * @Title: UmengDialog.java
- * @Package com.umeng.dp.builder
- * @Description: Builder模式
- * @author Honghui He
- * @version V1.0
- */
- public class UmengDialog {
- private String mTitle = "";
- private String mMessage = "";
- private int mIcon = -1;
- /**
- * @Title: UmengDialog
- * @Description: UmengDialog Constructor
- * @param builder 将dialog参数传递给builder,够将好的builder传递给Dialog
- */
- private UmengDialog(Builder builder) {
- mTitle = builder.getTitle();
- mMessage = builder.getMessage();
- mIcon = builder.getIcon();
- }
- /**
- * @Title: show
- * @Description: show dialog
- * @throws
- */
- public void show() {
- System.out.println("显示窗口 : " + this);
- }
- /**
- * (非 Javadoc)
- *
- * @Title: toString
- * @Description:
- * @return
- * @see java.lang.Object#toString()
- */
- @Override
- public String toString() {
- return "UmengDialog [mTitle=" + mTitle + ", mMessage=" + mMessage + ", mIcon=" + mIcon
- + "]";
- }
- /**
- * @ClassName: Builder
- * @Description: Builder模式, 用于构建复杂的对象
- * @author Honghui He
- */
- public static class Builder {
- private String mDlgTitle = "";
- private String mDlgMessage = "";
- private int mDlgIcon = -1;
- /**
- * @Title: Builder
- * @Description: Builder Constructor
- */
- public Builder() {
- }
- /**
- * 获取 mDlgTitle
- *
- * @return 返回 mDlgTitle
- */
- public String getTitle() {
- return mDlgTitle;
- }
- /**
- * 设置 mDlgTitle
- *
- * @param 对mDlgTitle进行赋值
- */
- public Builder setTitle(String title) {
- this.mDlgTitle = title;
- return this;
- }
- /**
- * 获取 mDlgMessage
- *
- * @return 返回 mDlgMessage
- */
- public String getMessage() {
- return mDlgMessage;
- }
- /**
- * 设置 mDlgMessage
- *
- * @param 对mDlgMessage进行赋值
- */
- public Builder setMessage(String msg) {
- this.mDlgMessage = msg;
- return this;
- }
- /**
- * 获取 mDlgIcon
- *
- * @return 返回 mDlgIcon
- */
- public int getIcon() {
- return mDlgIcon;
- }
- /**
- * 设置 mDlgIcon
- *
- * @param 对mDlgIcon进行赋值
- */
- public Builder setIcon(int resId) {
- this.mDlgIcon = resId;
- return this;
- }
- /**
- * @Title: create
- * @Description: 创建窗口
- * @throws
- */
- public UmengDialog create() {
- return new UmengDialog(this);
- }
- }
- }
输出结果 :
显示窗口 : UmengDialog [mTitle=Alert Dialg, mMessage=Builder, mIcon=123]
0 0
- 设计模式:Builder模式
- 设计模式 -- BUILDER模式
- 【设计模式】Builder模式
- 设计模式-Builder模式
- 设计模式Builder模式
- 设计模式-Builder模式
- 设计模式 - Builder模式
- 设计模式 - Builder模式
- 设计模式---Builder模式
- 设计模式--Builder模式
- 设计模式---Builder模式
- 设计模式--Builder模式
- 设计模式---Builder模式
- 设计模式---Builder模式
- 设计模式-builder模式
- 【设计模式】Builder模式
- 设计模式----Builder模式
- 设计模式-Builder模式
- web入门-Servlet与过滤器
- 深度学习edusoho之前端插件
- UICollectionView详解和UITableView的区别
- 关于动态操作select的option的值的问题
- Hadoop学习
- builder设计模式
- 雷特字幕2.35安装图文教程
- idea Context: local file . file is included in 3 contexts
- ios无法定位CLLocationManager Delegate方法不能被调用的问题
- OC中description、 SEL、类本质、self和super用法
- easyUI-tree
- DBA_UNDO_EXTENTS
- linux中千兆网的设置
- Swift-字典