介绍 建造者模式(英:Builder Pattern)是一种创建型设计模式,又名:生成器模式。GOF 给建造者模式的定义为:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。建造者模式负责按顺序创建复杂对象(把内部的建造过程和细节隐藏起来)。
结构
抽象建造者(Builder)角色 : 可以是一个接口或者抽象类,主要用于规范产品对象的各个组成成分的建造
具体建造者(ConcreteBuilder)角色 :
实现抽象建造者Builder所声明的接口
提供产品实例,用户构建产品
导演者(Director)角色 : 调用具体建造者角色以创建产品对象
产品(Product)角色 : 最终构建成具体产品
导演者角色是与客户端打交道的角色。导演者将客户端创建产品的请求划分为对各个零件的建造请求,再将这些请求委派给具体建造者角色。具体建造者角色是做具体建造工作的,但是却不为客户端所知
代码示例 产品 Product
1 2 3 4 5 6 7 8 9 10 11 12 public class Product { private String partA; private String partB; private String partC; }
抽象建造者 Builder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public interface Builder { void buildPartA () ; void buildPartB () ; void buildPartC () ; }
具体建造者 ConcreteBuilder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 public class ConcreteBuilder implements Builder { Product product = new Product(); @Override public void buildPartA () { product.setPartA(); } @Override public void buildPartB () { product.setPartB(); } @Override public void buildPartC () { product.setPartC(); } public Product getProduct () { return product; } }
导演者 Director
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class Director { private Builder builder; public Director (Builder builder) { this .builder = builder; } public void construct () { builder.buildPartA(); builder.buildPartB(); builder.buildPartC(); } }
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class Client { public static void main (String[] args) { Builder builder = new ConcreteBuilder(); Director director = new Director(builder); director.construct(); Product product = builder.getProduct(); } }
使用Builder模式来构建复杂对象
若User类,只有姓名(name) 和 手机号(phone) 是必选参数,其它都是可选参数,如果此刻我们不同的业务需要构建不同参数的User对象,我们可以使用不同参数的构造函数,那么我们的类中是这样
使用构造函数方式构造对象的User类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 public class User { private String name; private String phone; private String email; private int age; private Date birthDay; public User () { } public User (String name, String phone) { this .name = name; this .phone = phone; } public User (String name, String phone, String email) { this .name = name; this .phone = phone; this .email = email; } public User (String name, String phone, String email, int age) { this .name = name; this .phone = phone; this .email = email; this .age = age; } public User (String name, String phone, String email, int age, Date birthDay) { this .name = name; this .phone = phone; this .email = email; this .age = age; this .birthDay = birthDay; } }
测试代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class Cient { public static void main (String[] args) { User user1 = new User("admin" ,"110" ); User user2 = new User("admin" ,"110" ,"110@gmail.com" ); User user3 = new User("admin" ,"110" ,"110@gmail.com" ,110 ); } }
使用建造者模式来构建对象 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 public class User { private String name; private String phone; private String email; private int age; private Date birthDay; private User (UserBuilder builder) { this .name = builder.name; this .phone = builder.phone; this .email = builder.email; this .age = builder.age; this .birthDay = builder.birthDay; } public static class UserBuilder { private String name; private String phone; private String email; private int age; private Date birthDay; public UserBuilder (String name, String phone) { this .name = name; this .phone = phone; } public UserBuilder setEmail (String email) { this .email = email; return this ; } public UserBuilder setAge (int age) { this .age = age; return this ; } public UserBuilder setBirthDay (Date birthDay) { this .birthDay = birthDay; return this ; } public User build () { return new User(this ); } } }
测试代码 1 2 3 4 5 6 7 8 9 10 11 12 public class Cient { public static void main (String[] args) { User user = new User.UserBuilder("admin" ,"110" ) .setAge(100 ) .setEmail("110@gmail.com" ) .build(); } }