121-java-object-oriented-design
npx machina-cli add skill jabrena/cursor-rules-java/121-java-object-oriented-design --openclawJava Object-Oriented Design Guidelines
Review and improve Java code using comprehensive object-oriented design guidelines and refactoring practices.
Core areas: Fundamental design principles (SOLID, DRY, YAGNI), class and interface design (composition over inheritance, immutability, accessibility minimization, accessor methods), core OOP concepts (encapsulation, inheritance, polymorphism), object creation patterns (static factory methods, Builder pattern, Singleton, dependency injection, avoiding unnecessary objects), OOD code smells (God Class, Feature Envy, Inappropriate Intimacy, Refused Bequest, Shotgun Surgery, Data Clumps), method design (parameter validation, defensive copies, careful signatures, empty collections over nulls, Optional usage), and exception handling (checked vs. runtime exceptions, standard exceptions, failure-capture messages, no silent ignoring).
Prerequisites: Run ./mvnw compile or mvn compile before applying any change. If compilation fails, stop immediately and do not proceed — compilation failure is a blocking condition.
Multi-step scope: Step 1 validates the project compiles. Step 2 identifies applicable design principles and code smells. Step 3 applies SOLID/DRY/YAGNI improvements. Step 4 addresses class and interface design issues. Step 5 resolves OOP misuse and code smells. Step 6 improves object creation, method design, and exception handling patterns.
Before applying changes: Read the reference for detailed examples, good/bad patterns, and constraints.
Reference
For detailed guidance, examples, and constraints, see references/121-java-object-oriented-design.md.
Source
git clone https://github.com/jabrena/cursor-rules-java/blob/main/skills/121-java-object-oriented-design/SKILL.mdView on GitHub Overview
These guidelines help you review and improve Java code for object-oriented design quality. They cover SOLID, DRY, and YAGNI principles, class and interface design, and correct OOP concepts like encapsulation, inheritance, and polymorphism. They also address common smells (God Class, Feature Envy, Data Clumps) and patterns for object creation, method design, and exception handling.
How This Skill Works
Work begins with prerequisites: compile the project using mvnw or mvn. Step 2 identifies applicable design principles and smells, then Step 3 applies SOLID/DRY/YAGNI improvements. Subsequent steps address class/interface design, OOP misuse, and object creation, method design, and exception handling patterns.
When to Use It
- You're reviewing a Java module with God Class, Feature Envy, or other code smells and need systematic refactoring.
- You want to enforce SOLID, DRY, and YAGNI across a package or project.
- You need to shift from inheritance to composition and improve encapsulation.
- You’re standardizing object creation (Builders, static factories, DI) and refining exception handling.
- You’re refining method signatures, null handling, and defensive copying to improve robustness.
Quick Start
- Step 1: Run ./mvnw compile (or mvn compile) to ensure the project builds.
- Step 2: Scan for OOP smells (God Class, Data Clumps, Feature Envy) and SOLID/DRY/YAGNI gaps.
- Step 3: Apply targeted refactors following the guidelines in SKILL.md and the reference docs.
Best Practices
- Start by validating the project compiles (Prerequisite).
- Identify applicable design principles and code smells before making changes.
- Apply SOLID/DRY/YAGNI improvements in a structured, incremental way.
- Address class and interface design issues, favor composition over inheritance where appropriate.
- Improve object creation patterns, method design, and exception handling with clear, robust practices.
Example Use Cases
- Refactor a God Class into multiple cohesive classes with focused responsibilities.
- Replace inheritance with composition to reduce fragility and improve extensibility.
- Replace null returns with Optional and use empty collections instead of nulls.
- Introduce static factories or Builder patterns for complex object creation and dependency injection.
- Refactor exception handling to use meaningful messages and properly distinguish checked vs runtime exceptions.