设计模式-构建模式

来源:互联网 发布:mysql远程登录失败 编辑:程序博客网 时间:2024/05/16 07:55

设计模式之生成器模式

  生成器模式 也称 构建模式,是一种比较常用的模式。 比如 Android 中大家熟悉的AlertDialog 就是使用的构建器模式。比如 咱们投简历就会知道 导出 会有格式的选择,如 html ,word格式 这个也可以用到生成器模式 使得编码更加的规范。

 

生成器定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示

构建器模式的优点:同样的过程 可以获得不同的结果。构建过程可以复用。获取具体的结果可以灵活的切换,总结来说就是 松散耦合,代码复用


[java] view plain copy
  1. AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  2. builder.setTitle("温馨提示").setMessage("你打开了一个对话框").setIcon(R.drawable.ic_launcher);  
  3. builder.show();  

如上述代码  通过 AlertDialog.Builder 在show 或者 create的时候 都会返回一个AlertDialog对象。

下面通过一个小例子结合我们的模式和链式编程 让我们对构建模式更加了解

下面我们就构造一个人的对象 并且 可以进行一些操作


[java] view plain copy
  1. package com.ww.build;  
  2.   
  3. public class Person {  
  4.       
  5.     //名称  
  6.     private String name;  
  7.       
  8.     //身高  
  9.     private String height;  
  10.       
  11.     //性别  
  12.     private String sex;  
  13.       
  14.     //体重  
  15.     private String weight;  
  16.       
  17.     //身份证号码  
  18.     private String cardId;  
  19.       
  20.     public Person(String name, String height, String sex, String weight, String cardId) {  
  21.         super();  
  22.         this.name = name;  
  23.         this.height = height;  
  24.         this.sex = sex;  
  25.         this.weight = weight;  
  26.         this.cardId = cardId;  
  27.     }  
  28.       
  29.     public void show() {  
  30.         System.out.println("姓名:"+name+" 性别:"+sex+" 身高:"+height+" 体重:"+weight+ " 身份证号码:"+cardId);  
  31.     }  
  32.   
  33. }  

有了个person对象 那就下面就是它的构建器


[java] view plain copy
  1. package com.ww.build;  
  2.   
  3. public class ConcreteBuilder {  
  4.       
  5.     private String name;  
  6.     private String height;  
  7.     private String sex;  
  8.     private String weight;  
  9.     private String cardId;  
  10.       
  11.     public ConcreteBuilder(){  
  12.     }  
  13.       
  14.     public ConcreteBuilder(String name, String height, String sex, String weight, String cardId) {  
  15.         super();  
  16.         this.name = name;  
  17.         this.height = height;  
  18.         this.sex = sex;  
  19.         this.weight = weight;  
  20.         this.cardId = cardId;  
  21.     }  
  22.       
  23.     public Person build() {  
  24.         return new Person(name, height, sex, weight, cardId);  
  25.     }  
  26.       
  27.     public ConcreteBuilder setName(String name) {  
  28.         this.name = name;  
  29.         return this;  
  30.     }  
  31.       
  32.     public ConcreteBuilder setHeight(String height) {  
  33.         this.height = height;  
  34.         return this;  
  35.     }  
  36.       
  37.     public ConcreteBuilder setSex(String sex) {  
  38.         this.sex = sex;  
  39.         return this;  
  40.     }  
  41.       
  42.     public ConcreteBuilder setWeight(String weight) {  
  43.         this.weight = weight;  
  44.         return this;  
  45.     }  
  46.     public ConcreteBuilder setCardId(String cardId) {  
  47.         this.cardId = cardId;  
  48.         return this;  
  49.     }  
  50.   
  51. }  


下面就是测试代码


[java] view plain copy
  1. package com.ww.build;  
  2.   
  3. public class Test {  
  4.       
  5.     public static void main(String[] args) {  
  6.         ConcreteBuilder builder = new ConcreteBuilder();  
  7.         builder.setName("张三").setSex("男").setHeight("167cm");  
  8.         builder.setWeight("65kg").setCardId("430181199505065525");  
  9.         Person person = builder.build();  
  10.         person.show();  
  11.     }  
  12.   
  13. }  


相信通过这个例子 大家对 生成器模式和 链式编程有一定的了解。(生成器模式是一种设计模式,链式编程是编码的一种规范 两者不一定一起出现)
转载自http://blog.csdn.net/a3280028/article/details/51051770
原创粉丝点击