Java 模式¶
本文件使用 Java 特定内容扩展 common/patterns.md。
仓储模式¶
将数据访问封装在接口之后:
public interface OrderRepository {
Optional<Order> findById(Long id);
List<Order> findAll();
Order save(Order order);
void deleteById(Long id);
}
具体实现处理存储细节(JPA、JDBC、测试用内存实现)。
服务层¶
业务逻辑放在服务类中;保持控制器和仓储层轻薄:
public class OrderService {
private final OrderRepository orderRepository;
private final PaymentGateway paymentGateway;
public OrderService(OrderRepository orderRepository, PaymentGateway paymentGateway) {
this.orderRepository = orderRepository;
this.paymentGateway = paymentGateway;
}
public OrderSummary placeOrder(CreateOrderRequest request) {
var order = Order.from(request);
paymentGateway.charge(order.total());
var saved = orderRepository.save(order);
return OrderSummary.from(saved);
}
}
构造器注入¶
始终使用构造器注入 — 不要使用字段注入:
// GOOD — コンストラクタインジェクション(テスト可能、不変)
public class NotificationService {
private final EmailSender emailSender;
public NotificationService(EmailSender emailSender) {
this.emailSender = emailSender;
}
}
// BAD — フィールドインジェクション(リフレクションなしではテスト不可、フレームワークの魔法が必要)
public class NotificationService {
@Inject // or @Autowired
private EmailSender emailSender;
}
DTO 映射¶
DTO 使用 record。在服务/控制器边界进行映射:
public record OrderResponse(Long id, String customer, BigDecimal total) {
public static OrderResponse from(Order order) {
return new OrderResponse(order.getId(), order.getCustomerName(), order.getTotal());
}
}
Builder 模式¶
用于可选参数较多的对象:
public class SearchCriteria {
private final String query;
private final int page;
private final int size;
private final String sortBy;
private SearchCriteria(Builder builder) {
this.query = builder.query;
this.page = builder.page;
this.size = builder.size;
this.sortBy = builder.sortBy;
}
public static class Builder {
private String query = "";
private int page = 0;
private int size = 20;
private String sortBy = "id";
public Builder query(String query) { this.query = query; return this; }
public Builder page(int page) { this.page = page; return this; }
public Builder size(int size) { this.size = size; return this; }
public Builder sortBy(String sortBy) { this.sortBy = sortBy; return this; }
public SearchCriteria build() { return new SearchCriteria(this); }
}
}
领域模型的密封类型¶
public sealed interface PaymentResult permits PaymentSuccess, PaymentFailure {
record PaymentSuccess(String transactionId, BigDecimal amount) implements PaymentResult {}
record PaymentFailure(String errorCode, String message) implements PaymentResult {}
}
// 網羅的な処理(Java 21+)
String message = switch (result) {
case PaymentSuccess s -> "Paid: " + s.transactionId();
case PaymentFailure f -> "Failed: " + f.errorCode();
};
API 响应信封¶
一致的 API 响应:
public record ApiResponse<T>(boolean success, T data, String error) {
public static <T> ApiResponse<T> ok(T data) {
return new ApiResponse<>(true, data, null);
}
public static <T> ApiResponse<T> error(String message) {
return new ApiResponse<>(false, null, message);
}
}
参考资料¶
技能:springboot-patterns 用于参考 Spring Boot 架构模式。
技能:quarkus-patterns 用于参考包含 REST、Panache、消息传递的 Quarkus 架构模式。
技能:jpa-patterns 用于参考实体设计和查询优化。
本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 ev/ECC