ITPub博客

首页 > 应用开发 > Java > 设计模式之建造者模式(Builder)

设计模式之建造者模式(Builder)

原创 Java 作者:diy_os 时间:2016-11-03 16:59:48 0 删除 编辑
在文章之前,简单介绍下设计模式中的分类:
总体来说设计模式分为三大类:
创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。
结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。
行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。
以上也就是常说的23种设计模式,设计模式还有六大原则:
单一职责原则,里氏替换原则,依赖倒置原则,接口隔壁原则,迪米特法则,开闭原则
建造者模式属于创建型模式,在构建对象完成后返回生成的对象,并且将对象的构建与表示分离。

  1. package com.java.idea.builder;
  2. /**
  3.  * Created by diy_os on 2016/11/3.
  4.  */
  5. public class Person {
  6.     private int id;//身份证
  7.     private int age; //年龄
  8.     private String name; //姓名
  9.     private String address; //住址
  10.     private boolean Marry; //是否结婚
  11.     private String level; //水平

  12.     private Person(Builder builder) { //私有化显示类构造器
  13.         this.age = builder.age;
  14.         this.id = builder.id;
  15.         this.address = builder.address;
  16.         this.level = builder.level;
  17.         this.Marry = builder.Marry;
  18.         this.name = builder.name;
  19.     }

  20.     public int getAge() {
  21.         return age;
  22.     }

  23.     public int getId() {
  24.         return id;
  25.     }

  26.     public String getName() {
  27.         return name;
  28.     }

  29.     public String getAddress() {
  30.         return address;
  31.     }

  32.     public boolean getMarray() {
  33.         return Marry;
  34.     }

  35.     public String getLevel() {
  36.         return level;
  37.     }

  38.     public String toString() {
  39.         return "id=" + id + "\n" +
  40.                 "name=" + name + "\n"
  41.                 + "age=" + age + "\n" +
  42.                 "address=" + address + "\n" +
  43.                 "level=" + level + "\n" +
  44.                 "Marrry=" + Marry + "\n";
  45.     }

  46.     public static class Builder {
  47.         private int id;//身份证
  48.         private int age; //年龄
  49.         private String name; //姓名
  50.         private String address; //住址
  51.         private boolean Marry; //是否结婚
  52.         private String level; //水平

  53.         public Builder() {

  54.         }

  55.         public Builder setId(int id) {
  56.             this.id = id;
  57.             return this;
  58.         }

  59.         public Builder setAge(int age) {
  60.             this.age = age;
  61.             return this; //返回当前对象
  62.         }

  63.         public Builder setName(String name) {
  64.             this.name = name;
  65.             return this;
  66.         }

  67.         public Builder setAddress(String address) {
  68.             this.address = address;
  69.             return this;
  70.         }

  71.         public Builder setMarray(boolean marray) {
  72.             this.Marry = marray;
  73.             return this;
  74.         }

  75.         public Builder setLevel(String level) {
  76.             this.level = level;
  77.             return this;
  78.         }

  79.         public Person builder() {
  80.             return new Person(this);
  81.         }
  82.     }
  83. }
测试类:

点击(此处)折叠或打开

  1. package com.java.idea.builder;
  2. /**
  3.  * Created by diy_os on 2016/11/3.
  4.  */
  5. public class Main {
  6.     private String p;
  7.     private String q;

  8.     public Main setP(String p) {
  9.         this.p = p;
  10.         return this;
  11.     }

  12.     public Main setQ(String q) {
  13.         this.q = q;
  14.         return this;
  15.     }

  16.     public String toString() {
  17.         return "p=" + p + "\n" + "q=" + q + "\n";
  18.     }

  19.     public static void main(String[] args) {
  20.         Person person1 = new Person.Builder().setAddress("chongqing").setAge(22).setId(1).setLevel("3").setMarray(false).setName("diy_os").builder();
  21.         System.out.println(person1.getName());
  22.         System.out.print(person1);
  23.         Person person2 = new Person.Builder().setAddress("chongqing").setLevel("3").setMarray(false).setName("diy_os").builder();
  24.         System.out.print(person2);
  25.         Main main = new Main();
  26.         System.out.println(main.setP("p").setQ("q").toString());
  27.     }
  28. }



来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29876893/viewspace-2127630/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 端午杂谈
请登录后发表评论 登录
全部评论

注册时间:2014-12-11

  • 博文量
    159
  • 访问量
    985079