java-design-patterns

Implements creational, behavioral, and structural design patterns with Java and Spring examples.

Updated Apr 20, 2020
One-click install
npx skills add https://github.com/UnterrainerInformatik/java-rdb-utils --skill java-design-patterns-unterrainerinformatik
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: java-design-patterns
Source: https://github.com/UnterrainerInformatik/java-rdb-utils/tree/main/.agents/skills/java-design-patterns
Command: npx skills add https://github.com/UnterrainerInformatik/java-rdb-utils --skill java-design-patterns-unterrainerinformatik

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Choosing and correctly implementing the right design pattern in Java is error-prone, and developers often struggle with verbose boilerplate, rigid code structures, and knowing which pattern fits a given problem. ## Core Features & Use Cases - Pattern Reference with Code: Provides working Java implementations of Builder, Factory Method, Singleton, Strategy, Observer, Template Method, Decorator, and Adapter patterns. - Spring Integration: Shows Spring-native alternatives such as component-based factories, ApplicationEventPublisher for Observer, and dependency injection instead of Singletons. - Selection Guidance: Includes quick-reference tables mapping problems (e.g., many constructor parameters, runtime algorithm swapping) to the appropriate pattern, plus anti-patterns to avoid. - Use Case: When asked to support multiple payment methods swappable at runtime, the Skill provides the Strategy pattern implementation with both classic and functional Java 8+ variants. ## Quick Start Ask the assistant to implement the factory pattern for creating different notification types in Java.

Frequently Asked Questions about java-design-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement the Builder pattern in Java?▼

Create a static nested Builder class with required fields in its constructor and fluent setter methods for optional fields, ending with a build() method. With Lombok, the @Builder annotation generates this automatically, using @Builder.Default for default values.

When should I use Strategy vs Factory pattern in Java?▼

Use Factory when you need to create objects without specifying the exact class at the call site. Use Strategy when you have multiple algorithms for the same operation and need to swap them at runtime, such as switching payment methods.

How do I implement the Observer pattern with Spring?▼

Publish events using ApplicationEventPublisher and create listener methods annotated with @EventListener in Spring components. Add @Async to listeners for asynchronous event processing instead of manually maintaining observer lists.

Is the Singleton pattern recommended in Spring applications?▼

Singletons are discouraged because they create global state and hidden dependencies that complicate testing. In Spring, prefer @Component beans, which are singleton-scoped by default and managed through dependency injection.

What are the limitations of the Decorator pattern?▼

Deep decorator chains become hard to debug and reason about since behavior is spread across many wrapper objects. Keep chains short and consider composition or alternative designs when stacking many decorators.