builder设计模式

来源:互联网 发布:教育软件开发 编辑:程序博客网 时间:2024/06/04 11:01

本文转自:http://blog.csdn.net/bboyfeiyu/article/details/15504187

   在Android中经常要使用AlertDialog来显示一些简单的窗口,通常的写法类似下面的代码 : 

[java] view plain copy
  1. AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("提示")  
  2.         .setMessage("确定退出?").create();  
  3. dialog.show();  
可以看到dialog是通过Builder创建的,那么Builder是如何同AlertDialog协同工作的呢?其实AlertDialog的创建就是使用Builder模式,废话不多说,直接上代码吧。


[java] view plain copy
  1. package com.umeng.dp.builder;  
  2.   
  3. /** 
  4.  * @Copyright: Umeng.com, Ltd. Copyright 2011-2015, All rights reserved 
  5.  * @Title: UmengDialog.java 
  6.  * @Package com.umeng.dp.builder 
  7.  * @Description: Builder模式 
  8.  * @author Honghui He 
  9.  * @version V1.0 
  10.  */  
  11.   
  12. public class UmengDialog {  
  13.   
  14.     private String mTitle = "";  
  15.     private String mMessage = "";  
  16.     private int mIcon = -1;  
  17.   
  18.     /** 
  19.      * @Title: UmengDialog 
  20.      * @Description: UmengDialog Constructor 
  21.      * @param builder 将dialog参数传递给builder,够将好的builder传递给Dialog 
  22.      */  
  23.     private UmengDialog(Builder builder) {  
  24.         mTitle = builder.getTitle();  
  25.         mMessage = builder.getMessage();  
  26.         mIcon = builder.getIcon();  
  27.     }  
  28.   
  29.     /** 
  30.      * @Title: show 
  31.      * @Description: show dialog 
  32.      * @throws 
  33.      */  
  34.     public void show() {  
  35.         System.out.println("显示窗口 : " + this);  
  36.     }  
  37.   
  38.     /** 
  39.      * (非 Javadoc) 
  40.      *  
  41.      * @Title: toString 
  42.      * @Description: 
  43.      * @return 
  44.      * @see java.lang.Object#toString() 
  45.      */  
  46.     @Override  
  47.     public String toString() {  
  48.         return "UmengDialog [mTitle=" + mTitle + ", mMessage=" + mMessage + ", mIcon=" + mIcon  
  49.                 + "]";  
  50.     }  
  51.   
  52.     /** 
  53.      * @ClassName: Builder 
  54.      * @Description: Builder模式, 用于构建复杂的对象 
  55.      * @author Honghui He 
  56.      */  
  57.     public static class Builder {  
  58.         private String mDlgTitle = "";  
  59.         private String mDlgMessage = "";  
  60.         private int mDlgIcon = -1;  
  61.   
  62.         /** 
  63.          * @Title: Builder 
  64.          * @Description: Builder Constructor 
  65.          */  
  66.         public Builder() {  
  67.         }  
  68.   
  69.         /** 
  70.          * 获取 mDlgTitle 
  71.          *  
  72.          * @return 返回 mDlgTitle 
  73.          */  
  74.         public String getTitle() {  
  75.             return mDlgTitle;  
  76.         }  
  77.   
  78.         /** 
  79.          * 设置 mDlgTitle 
  80.          *  
  81.          * @param 对mDlgTitle进行赋值 
  82.          */  
  83.         public Builder setTitle(String title) {  
  84.             this.mDlgTitle = title;  
  85.             return this;  
  86.         }  
  87.   
  88.         /** 
  89.          * 获取 mDlgMessage 
  90.          *  
  91.          * @return 返回 mDlgMessage 
  92.          */  
  93.         public String getMessage() {  
  94.             return mDlgMessage;  
  95.         }  
  96.   
  97.         /** 
  98.          * 设置 mDlgMessage 
  99.          *  
  100.          * @param 对mDlgMessage进行赋值 
  101.          */  
  102.         public Builder setMessage(String msg) {  
  103.             this.mDlgMessage = msg;  
  104.             return this;  
  105.         }  
  106.   
  107.         /** 
  108.          * 获取 mDlgIcon 
  109.          *  
  110.          * @return 返回 mDlgIcon 
  111.          */  
  112.         public int getIcon() {  
  113.             return mDlgIcon;  
  114.         }  
  115.   
  116.         /** 
  117.          * 设置 mDlgIcon 
  118.          *  
  119.          * @param 对mDlgIcon进行赋值 
  120.          */  
  121.         public Builder setIcon(int resId) {  
  122.             this.mDlgIcon = resId;  
  123.             return this;  
  124.         }  
  125.   
  126.         /** 
  127.          * @Title: create 
  128.          * @Description: 创建窗口 
  129.          * @throws 
  130.          */  
  131.         public UmengDialog create() {  
  132.             return new UmengDialog(this);  
  133.         }  
  134.     }  
  135.   
  136. }  

输出结果 : 

显示窗口 : UmengDialog [mTitle=Alert Dialg, mMessage=Builder, mIcon=123]


0 0
原创粉丝点击