$npx -y skills add decebals/claude-code-java --skill design-patternsCommon design patterns with Java examples (Factory, Builder, Strategy, Observer, Decorator, etc.). Use when user asks "implement pattern", "use factory", "strategy pattern", or when designing extensible components.
| 1 | # Design Patterns Skill |
| 2 | |
| 3 | Practical design patterns reference for Java with modern examples. |
| 4 | |
| 5 | ## When to Use |
| 6 | - User asks to implement a specific pattern |
| 7 | - Designing extensible/flexible components |
| 8 | - Refactoring rigid code structures |
| 9 | - Code review suggests pattern usage |
| 10 | |
| 11 | --- |
| 12 | |
| 13 | ## Quick Reference: When to Use What |
| 14 | |
| 15 | | Problem | Pattern | |
| 16 | |---------|---------| |
| 17 | | Complex object construction | **Builder** | |
| 18 | | Create objects without specifying class | **Factory** | |
| 19 | | Multiple algorithms, swap at runtime | **Strategy** | |
| 20 | | Add behavior without changing class | **Decorator** | |
| 21 | | Notify multiple objects of changes | **Observer** | |
| 22 | | Ensure single instance | **Singleton** | |
| 23 | | Convert incompatible interfaces | **Adapter** | |
| 24 | | Define algorithm skeleton | **Template Method** | |
| 25 | |
| 26 | --- |
| 27 | |
| 28 | ## Creational Patterns |
| 29 | |
| 30 | ### Builder |
| 31 | |
| 32 | **Use when:** Object has many parameters, some optional. |
| 33 | |
| 34 | ```java |
| 35 | // ❌ Telescoping constructor antipattern |
| 36 | public class User { |
| 37 | public User(String name) { } |
| 38 | public User(String name, String email) { } |
| 39 | public User(String name, String email, int age) { } |
| 40 | public User(String name, String email, int age, String phone) { } |
| 41 | // ... explosion of constructors |
| 42 | } |
| 43 | |
| 44 | // ✅ Builder pattern |
| 45 | public class User { |
| 46 | private final String name; // required |
| 47 | private final String email; // required |
| 48 | private final int age; // optional |
| 49 | private final String phone; // optional |
| 50 | private final String address; // optional |
| 51 | |
| 52 | private User(Builder builder) { |
| 53 | this.name = builder.name; |
| 54 | this.email = builder.email; |
| 55 | this.age = builder.age; |
| 56 | this.phone = builder.phone; |
| 57 | this.address = builder.address; |
| 58 | } |
| 59 | |
| 60 | public static Builder builder(String name, String email) { |
| 61 | return new Builder(name, email); |
| 62 | } |
| 63 | |
| 64 | public static class Builder { |
| 65 | // Required |
| 66 | private final String name; |
| 67 | private final String email; |
| 68 | // Optional with defaults |
| 69 | private int age = 0; |
| 70 | private String phone = ""; |
| 71 | private String address = ""; |
| 72 | |
| 73 | private Builder(String name, String email) { |
| 74 | this.name = name; |
| 75 | this.email = email; |
| 76 | } |
| 77 | |
| 78 | public Builder age(int age) { |
| 79 | this.age = age; |
| 80 | return this; |
| 81 | } |
| 82 | |
| 83 | public Builder phone(String phone) { |
| 84 | this.phone = phone; |
| 85 | return this; |
| 86 | } |
| 87 | |
| 88 | public Builder address(String address) { |
| 89 | this.address = address; |
| 90 | return this; |
| 91 | } |
| 92 | |
| 93 | public User build() { |
| 94 | return new User(this); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Usage |
| 100 | User user = User.builder("John", "john@example.com") |
| 101 | .age(30) |
| 102 | .phone("+1234567890") |
| 103 | .build(); |
| 104 | ``` |
| 105 | |
| 106 | **With Lombok:** |
| 107 | ```java |
| 108 | @Builder |
| 109 | @Getter |
| 110 | public class User { |
| 111 | private final String name; |
| 112 | private final String email; |
| 113 | @Builder.Default private int age = 0; |
| 114 | private String phone; |
| 115 | } |
| 116 | ``` |
| 117 | |
| 118 | --- |
| 119 | |
| 120 | ### Factory Method |
| 121 | |
| 122 | **Use when:** Need to create objects without specifying exact class. |
| 123 | |
| 124 | ```java |
| 125 | // ✅ Factory Method pattern |
| 126 | public interface Notification { |
| 127 | void send(String message); |
| 128 | } |
| 129 | |
| 130 | public class EmailNotification implements Notification { |
| 131 | @Override |
| 132 | public void send(String message) { |
| 133 | System.out.println("Email: " + message); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | public class SmsNotification implements Notification { |
| 138 | @Override |
| 139 | public void send(String message) { |
| 140 | System.out.println("SMS: " + message); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | public class PushNotification implements Notification { |
| 145 | @Override |
| 146 | public void send(String message) { |
| 147 | System.out.println("Push: " + message); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // Factory |
| 152 | public class NotificationFactory { |
| 153 | |
| 154 | public static Notification create(String type) { |
| 155 | return switch (type.toUpperCase()) { |
| 156 | case "EMAIL" -> new EmailNotification(); |
| 157 | case "SMS" -> new SmsNotification(); |
| 158 | case "PUSH" -> new PushNotification(); |
| 159 | default -> throw new IllegalArgumentException("Unknown type: " + type); |
| 160 | }; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Usage |
| 165 | Notification notification = NotificationFactory.create("EMAIL"); |
| 166 | notification.send("Hello!"); |
| 167 | ``` |
| 168 | |
| 169 | **With Spring (preferred):** |
| 170 | ```java |
| 171 | public interface NotificationSender { |
| 172 | void send(String message); |
| 173 | String getType(); |
| 174 | } |
| 175 | |
| 176 | @Component |
| 177 | public class EmailSender implements NotificationSender { |
| 178 | @Override public void send(String message) { /* ... */ } |
| 179 | @Override public String getType() { return "EMAIL"; } |
| 180 | } |
| 181 | |
| 182 | @Component |
| 183 | public class SmsSender implements NotificationSender { |
| 184 | @Override public void send(String message) { /* ... */ } |
| 185 | @Override public String getType() { return "SMS"; } |
| 186 | } |
| 187 | |
| 188 | @Component |
| 189 | public class NotificationFactory { |
| 190 | private final Map<String, NotificationSender> sende |