$npx -y skills add ducpm2303/claude-java-plugins --skill java-design-patternDetects GoF patterns in Java code or recommends the right pattern for a problem. Use when user asks to "what pattern is this", "detect design patterns", "suggest a pattern", "should I use factory", "which design pattern", or "recommend a pattern for".
| 1 | Either detect which design patterns are used in the provided code, or recommend the right pattern for a described problem. Tailor all examples to the detected Java version (Java 8+). |
| 2 | |
| 3 | ## Mode A — Detect patterns in existing code |
| 4 | Scan the code for these common patterns and name them explicitly: |
| 5 | |
| 6 | **Creational:** |
| 7 | - **Singleton** — private constructor + static instance (flag if not thread-safe without `volatile` or `enum`) |
| 8 | - **Factory Method** — static `create()` or `of()` returning an interface type |
| 9 | - **Builder** — inner static `Builder` class with fluent setters and `build()` |
| 10 | - **Abstract Factory** — factory that creates families of related objects |
| 11 | |
| 12 | **Structural:** |
| 13 | - **Decorator** — wraps another object implementing the same interface to add behaviour |
| 14 | - **Proxy** — Spring `@Transactional`, `@Cacheable`, `@Async` are all proxies; flag manual proxies |
| 15 | - **Adapter** — converts one interface to another (often with `*Adapter` or `*Wrapper` class name) |
| 16 | - **Facade** — simplifies a complex subsystem; often a service that orchestrates multiple repos/clients |
| 17 | - **Composite** — tree structure where leaf and composite implement the same interface |
| 18 | |
| 19 | **Behavioral:** |
| 20 | - **Strategy** — interface with multiple implementations selected at runtime |
| 21 | - **Observer** — Spring `ApplicationEvent` / `@EventListener` or manual listener list |
| 22 | - **Template Method** — abstract class with `final` algorithm method calling overridable steps |
| 23 | - **Command** — encapsulates a request as an object (common in undo/redo, queuing) |
| 24 | - **Chain of Responsibility** — Spring Security filter chain, Servlet filters |
| 25 | - **State** — object behaviour changes based on internal state enum/object |
| 26 | |
| 27 | For each detected pattern: name it, show the key code that reveals it, explain how it's used here. |
| 28 | |
| 29 | ## Mode B — Recommend a pattern for a problem |
| 30 | When the user describes a problem, recommend the most appropriate pattern: |
| 31 | |
| 32 | | Problem | Recommended Pattern | |
| 33 | |---|---| |
| 34 | | Need multiple algorithms interchangeable at runtime | Strategy | |
| 35 | | Object creation is complex, many optional fields | Builder | |
| 36 | | Need to add behaviour without modifying existing class | Decorator | |
| 37 | | Need to notify multiple objects when state changes | Observer | |
| 38 | | Complex subsystem needs a simple interface | Facade | |
| 39 | | Same operation on tree structures | Composite | |
| 40 | | Object has distinct lifecycle states | State | |
| 41 | | Need only one instance globally | Singleton (or Spring `@Component`) | |
| 42 | |
| 43 | Show a minimal Java implementation for the recommended pattern, appropriate for the detected Java version: |
| 44 | - Java 8+: use functional interfaces (Strategy can be a lambda `Function<T,R>`) |
| 45 | - Java 17+: use sealed classes for State pattern |
| 46 | - Java 16+: use records for value objects in patterns |
| 47 | |
| 48 | ## Output format |
| 49 | **Mode A:** List each detected pattern with: name + location + explanation + any concerns (e.g., non-thread-safe Singleton) |
| 50 | **Mode B:** Recommend pattern + minimal before/after code + when to use vs alternatives |
| 51 | |
| 52 | ## Next Steps |
| 53 | - After identifying patterns → run `/java-solid` to check if patterns are applied correctly |
| 54 | - For implementation help → ask the `java-architect` agent for full design guidance |