本文目录导读:

Java中生成器模式的实现案例
生成器模式(Builder Pattern)是一种创建型设计模式,用于构建复杂对象,特别是当对象有很多可选参数时。
经典生成器模式实现(以用户类为例)
public class User {
// 必选参数
private final String firstName;
private final String lastName;
// 可选参数
private final int age;
private final String phone;
private final String address;
private final String email;
// 私有构造方法,接收Builder对象
private User(UserBuilder builder) {
this.firstName = builder.firstName;
this.lastName = builder.lastName;
this.age = builder.age;
this.phone = builder.phone;
this.address = builder.address;
this.email = builder.email;
}
// Getter方法
public String getFirstName() { return firstName; }
public String getLastName() { return lastName; }
public int getAge() { return age; }
public String getPhone() { return phone; }
public String getAddress() { return address; }
public String getEmail() { return email; }
// 静态内部Builder类
public static class UserBuilder {
// 必选参数
private final String firstName;
private final String lastName;
// 可选参数(设置默认值)
private int age = 0;
private String phone = "";
private String address = "";
private String email = "";
// Builder构造方法接收必选参数
public UserBuilder(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
// 设置可选参数(返回Builder本身支持链式调用)
public UserBuilder age(int age) {
this.age = age;
return this;
}
public UserBuilder phone(String phone) {
this.phone = phone;
return this;
}
public UserBuilder address(String address) {
this.address = address;
return this;
}
public UserBuilder email(String email) {
this.email = email;
return this;
}
// 构建方法,创建User实例
public User build() {
// 可以在这里添加参数验证逻辑
if (age < 0) {
throw new IllegalArgumentException("年龄不能为负数");
}
return new User(this);
}
}
}
使用示例
public class BuilderPatternDemo {
public static void main(String[] args) {
// 创建用户对象(只设置必选参数)
User user1 = new User.UserBuilder("张三", "李")
.build();
// 创建用户对象(设置所有参数)
User user2 = new User.UserBuilder("王", "五")
.age(28)
.phone("13800138000")
.address("北京市朝阳区")
.email("wangwu@example.com")
.build();
// 创建用户对象(部分可选参数)
User user3 = new User.UserBuilder("赵", "六")
.age(25)
.email("zhaoliu@example.com")
.build();
System.out.println("用户1: " + user1.getFirstName() + " " + user1.getLastName());
System.out.println("用户2: " + user2.getFirstName() + " " + user2.getLastName() +
", 年龄: " + user2.getAge());
System.out.println("用户3: " + user3.getFirstName() + " " + user3.getLastName() +
", 年龄: " + user3.getAge() + ", 邮箱: " + user3.getEmail());
}
}
带验证的生成器模式
public class Order {
private final String orderId;
private final String customerName;
private final List<String> items;
private final double totalAmount;
private final String shippingAddress;
private final String paymentMethod;
private Order(OrderBuilder builder) {
this.orderId = builder.orderId;
this.customerName = builder.customerName;
this.items = builder.items;
this.totalAmount = builder.totalAmount;
this.shippingAddress = builder.shippingAddress;
this.paymentMethod = builder.paymentMethod;
}
public static class OrderBuilder {
private String orderId;
private String customerName;
private List<String> items = new ArrayList<>();
private double totalAmount = 0.0;
private String shippingAddress;
private String paymentMethod = "现金";
public OrderBuilder orderId(String orderId) {
this.orderId = orderId;
return this;
}
public OrderBuilder customerName(String customerName) {
this.customerName = customerName;
return this;
}
public OrderBuilder addItem(String item) {
this.items.add(item);
return this;
}
public OrderBuilder items(List<String> items) {
this.items = new ArrayList<>(items);
return this;
}
public OrderBuilder totalAmount(double totalAmount) {
this.totalAmount = totalAmount;
return this;
}
public OrderBuilder shippingAddress(String shippingAddress) {
this.shippingAddress = shippingAddress;
return this;
}
public OrderBuilder paymentMethod(String paymentMethod) {
this.paymentMethod = paymentMethod;
return this;
}
public Order build() {
// 参数验证
if (orderId == null || orderId.isEmpty()) {
throw new IllegalStateException("订单ID不能为空");
}
if (customerName == null || customerName.isEmpty()) {
throw new IllegalStateException("客户名称不能为空");
}
if (items.isEmpty()) {
throw new IllegalStateException("订单必须包含至少一个商品");
}
if (totalAmount <= 0) {
throw new IllegalStateException("订单金额必须大于0");
}
if (shippingAddress == null || shippingAddress.isEmpty()) {
throw new IllegalStateException("收货地址不能为空");
}
return new Order(this);
}
}
// Getter方法
public String getOrderId() { return orderId; }
public String getCustomerName() { return customerName; }
public List<String> getItems() { return items; }
public double getTotalAmount() { return totalAmount; }
public String getShippingAddress() { return shippingAddress; }
public String getPaymentMethod() { return paymentMethod; }
}
// 使用示例
Order order = new Order.OrderBuilder()
.orderId("ORD-001")
.customerName("张三")
.addItem("笔记本电脑")
.addItem("鼠标")
.totalAmount(9999.99)
.shippingAddress("北京市海淀区")
.paymentMethod("支付宝")
.build();
Lombok @Builder 注解实现
import lombok.Builder;
import lombok.ToString;
@Builder
@ToString
public class Product {
private String name;
private double price;
private String category;
private String description;
private int stock;
private String manufacturer;
private double weight;
}
// 使用示例
Product product = Product.builder()
.name("智能手表")
.price(2999.99)
.category("电子产品")
.description("最新款智能手表")
.stock(100)
.manufacturer("华强北科技")
.weight(0.5)
.build();
System.out.println(product);
接口生成器模式(适用于复杂对象层次结构)
// 构建器接口
interface PizzaBuilder {
PizzaBuilder setDough(String dough);
PizzaBuilder setSauce(String sauce);
PizzaBuilder addTopping(String topping);
Pizza build();
}
// 具体产品
class Pizza {
private String dough;
private String sauce;
private List<String> toppings;
Pizza(String dough, String sauce, List<String> toppings) {
this.dough = dough;
this.sauce = sauce;
this.toppings = toppings;
}
@Override
public String toString() {
return "Pizza{" +
"dough='" + dough + '\'' +
", sauce='" + sauce + '\'' +
", toppings=" + toppings +
'}';
}
}
// 具体构建器
class HawaiianPizzaBuilder implements PizzaBuilder {
private String dough;
private String sauce;
private List<String> toppings = new ArrayList<>();
@Override
public PizzaBuilder setDough(String dough) {
this.dough = dough;
return this;
}
@Override
public PizzaBuilder setSauce(String sauce) {
this.sauce = sauce;
return this;
}
@Override
public PizzaBuilder addTopping(String topping) {
this.toppings.add(topping);
return this;
}
@Override
public Pizza build() {
return new Pizza(dough, sauce, toppings);
}
}
// 使用示例
Pizza hawaiianPizza = new HawaiianPizzaBuilder()
.setDough("薄底")
.setSauce("番茄酱")
.addTopping("菠萝")
.addTopping("火腿")
.build();
System.out.println(hawaiianPizza);
生成器模式的核心要点
- 私有构造方法:防止直接创建对象
- 静态内部Builder类:管理参数设置
- 链式调用:Builder方法返回this
- 不可变对象:创建后参数不可修改
- 参数验证:在build()方法中进行
适用场景
- 对象有多个必选和可选参数
- 参数之间可能存在依赖关系
- 需要创建不可变对象
- 构造方法参数过多(超过4个)
生成器模式特别适合Java Bean的创建,尤其是在需要创建复杂、多配置的对象时。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播