在文章之前,简单介绍下设计模式中的分类:
总体来说设计模式分为三大类:
创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。
结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。
行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。
以上也就是常说的23种设计模式,设计模式还有六大原则:
单一职责原则,
里氏替换原则,
依赖倒置原则,
接口隔壁原则,
迪米特法则,
开闭原则
建造者模式属于创建型模式,在构建对象完成后返回生成的对象,并且将对象的构建与表示分离。
简单的看下图例:
- /**
- * Created by diy_os on 2016/11/3.
- */
- public class Person {
- private int id;//身份证
- private int age; //年龄
- private String name; //姓名
- private String address; //住址
- private boolean Marry; //是否结婚
- private String level; //水平
- private Person(Builder builder) { //私有化显示类构造器
- this.age = builder.age;
- this.id = builder.id;
- this.address = builder.address;
- this.level = builder.level;
- this.Marry = builder.Marry;
- this.name = builder.name;
- }
- public int getAge() {
- return age;
- }
- public int getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- public String getAddress() {
- return address;
- }
- public boolean getMarray() {
- return Marry;
- }
- public String getLevel() {
- return level;
- }
- public String toString() {
- return "id=" + id + "\n" +
- "name=" + name + "\n"
- + "age=" + age + "\n" +
- "address=" + address + "\n" +
- "level=" + level + "\n" +
- "Marrry=" + Marry + "\n";
- }
- public static class Builder {
- private int id;//身份证
- private int age; //年龄
- private String name; //姓名
- private String address; //住址
- private boolean Marry; //是否结婚
- private String level; //水平
- public Builder() {
- }
- public Builder setId(int id) {
- this.id = id;
- return this;
- }
- public Builder setAge(int age) {
- this.age = age;
- return this; //返回当前对象
- }
- public Builder setName(String name) {
- this.name = name;
- return this;
- }
- public Builder setAddress(String address) {
- this.address = address;
- return this;
- }
- public Builder setMarray(boolean marray) {
- this.Marry = marray;
- return this;
- }
- public Builder setLevel(String level) {
- this.level = level;
- return this;
- }
- public Person builder() {
- return new Person(this);
- }
- }
- }
测试类:
- /**
- * Created by diy_os on 2016/11/3.
- */
- public class Main {
- private String p;
- private String q;
- public Main setP(String p) {
- this.p = p;
- return this;
- }
- public Main setQ(String q) {
- this.q = q;
- return this;
- }
- public String toString() {
- return "p=" + p + "\n" + "q=" + q + "\n";
- }
- public static void main(String[] args) {
- Person person1 = new Person.Builder().setAddress("chongqing").setAge(22).setId(1).setLevel("3").setMarray(false).setName("diy_os").builder();
- System.out.println(person1.getName());
- System.out.print(person1);
- Person person2 = new Person.Builder().setAddress("chongqing").setLevel("3").setMarray(false).setName("diy_os").builder();
- System.out.print(person2);
- Main main = new Main();
- System.out.println(main.setP("p").setQ("q").toString());
- }
- }
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29876893/viewspace-2140449/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29876893/viewspace-2140449/