smithery.ai

java-best-practices

Java 编码最佳实践与设计模式

First seen Mar 31, 2026

Installation

$ npx skills add https://smithery.ai

Similar popular skills

Related neighbors and high-traction skills in the same topics — useful to compare before installing.

Also in this package

Other skills from smithery.ai · top by installs.

npx skills add https://smithery.ai

Browse all from smithery.ai

More details

Agent compatibility

Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.

Claude Code Not declared
Cursor Not declared
Codex Not declared
GitHub Copilot Not declared
Windsurf Not declared
Gemini CLI Not declared
Cline Not declared
OpenCode Not declared

Skill metadata

Parsed from SKILL.md frontmatter.

Version1.0.0

Package contents

Files included with this skill beyond the listing page.

  • skill md SKILL.md 1,751 B
  • docs SUMMARY.md 65 B

History

  1. First seen on skills.sh
  2. First recorded snapshot · 3 installs

SKILL.md

Java 最佳实践技能包

编码规范

命名规范

  • 类名:PascalCase(UserService)
  • 方法/变量:camelCase(getUserById)
  • 常量:UPPERSNAKECASE(MAX_SIZE)
  • 包名:小写(com.example.service)

常用设计模式

单例模式(枚举实现)

public 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();
        };
    }
}

Builder 模式

User user = User.builder()
    .name("张三")
    .age(25)
    .build();

Stream API

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 使用

Optional<User> user = userRepository.findById(id);
return user.orElseThrow(() -> new NotFoundException());