Loading...
Loading...
Java 编码最佳实践与设计模式
npx skill4agent add leavesfly/jimi java-best-practicespublic enum Singleton {
INSTANCE;
public void doSomething() {}
}public class UserFactory {
public static User createUser(String type) {
return switch (type) {
case "admin" -> new AdminUser();
case "guest" -> new GuestUser();
default -> new RegularUser();
};
}
}User user = User.builder()
.name("张三")
.age(25)
.build();List<String> names = users.stream()
.filter(u -> u.getAge() > 18)
.map(User::getName)
.collect(Collectors.toList());try {
// 业务逻辑
} catch (SpecificException e) {
log.error("Error: {}", e.getMessage(), e);
throw new BusinessException("操作失败");
} finally {
// 清理资源
}ExecutorService executor = Executors.newFixedThreadPool(10);
executor.submit(() -> {
// 异步任务
});Optional<User> user = userRepository.findById(id);
return user.orElseThrow(() -> new NotFoundException());