@booklib/core 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (374) hide show
  1. package/.cursor/rules/booklib-standards.mdc +40 -0
  2. package/.gemini/context.md +372 -0
  3. package/AGENTS.md +166 -0
  4. package/CHANGELOG.md +226 -0
  5. package/CLAUDE.md +81 -0
  6. package/CODE_OF_CONDUCT.md +31 -0
  7. package/CONTRIBUTING.md +304 -0
  8. package/LICENSE +21 -0
  9. package/PLAN.md +28 -0
  10. package/README.ja.md +198 -0
  11. package/README.ko.md +198 -0
  12. package/README.md +503 -0
  13. package/README.pt-BR.md +198 -0
  14. package/README.uk.md +241 -0
  15. package/README.zh-CN.md +198 -0
  16. package/SECURITY.md +9 -0
  17. package/agents/architecture-reviewer.md +136 -0
  18. package/agents/booklib-reviewer.md +90 -0
  19. package/agents/data-reviewer.md +107 -0
  20. package/agents/jvm-reviewer.md +146 -0
  21. package/agents/python-reviewer.md +128 -0
  22. package/agents/rust-reviewer.md +115 -0
  23. package/agents/ts-reviewer.md +110 -0
  24. package/agents/ui-reviewer.md +117 -0
  25. package/assets/logo.svg +36 -0
  26. package/bin/booklib-mcp.js +304 -0
  27. package/bin/booklib.js +1705 -0
  28. package/bin/skills.cjs +1292 -0
  29. package/booklib-router.mdc +36 -0
  30. package/booklib.config.json +19 -0
  31. package/commands/animation-at-work.md +10 -0
  32. package/commands/clean-code-reviewer.md +10 -0
  33. package/commands/data-intensive-patterns.md +10 -0
  34. package/commands/data-pipelines.md +10 -0
  35. package/commands/design-patterns.md +10 -0
  36. package/commands/domain-driven-design.md +10 -0
  37. package/commands/effective-java.md +10 -0
  38. package/commands/effective-kotlin.md +10 -0
  39. package/commands/effective-python.md +10 -0
  40. package/commands/effective-typescript.md +10 -0
  41. package/commands/kotlin-in-action.md +10 -0
  42. package/commands/lean-startup.md +10 -0
  43. package/commands/microservices-patterns.md +10 -0
  44. package/commands/programming-with-rust.md +10 -0
  45. package/commands/refactoring-ui.md +10 -0
  46. package/commands/rust-in-action.md +10 -0
  47. package/commands/skill-router.md +10 -0
  48. package/commands/spring-boot-in-action.md +10 -0
  49. package/commands/storytelling-with-data.md +10 -0
  50. package/commands/system-design-interview.md +10 -0
  51. package/commands/using-asyncio-python.md +10 -0
  52. package/commands/web-scraping-python.md +10 -0
  53. package/community/registry.json +1616 -0
  54. package/hooks/hooks.json +23 -0
  55. package/hooks/posttooluse-capture.mjs +67 -0
  56. package/hooks/suggest.js +153 -0
  57. package/lib/agent-behaviors.js +40 -0
  58. package/lib/agent-detector.js +96 -0
  59. package/lib/config-loader.js +39 -0
  60. package/lib/conflict-resolver.js +148 -0
  61. package/lib/context-builder.js +574 -0
  62. package/lib/discovery-engine.js +298 -0
  63. package/lib/doctor/hook-installer.js +83 -0
  64. package/lib/doctor/usage-tracker.js +87 -0
  65. package/lib/engine/ai-features.js +253 -0
  66. package/lib/engine/auditor.js +103 -0
  67. package/lib/engine/bm25-index.js +178 -0
  68. package/lib/engine/capture.js +120 -0
  69. package/lib/engine/corrections.js +198 -0
  70. package/lib/engine/doctor.js +195 -0
  71. package/lib/engine/graph-injector.js +137 -0
  72. package/lib/engine/graph.js +161 -0
  73. package/lib/engine/handoff.js +405 -0
  74. package/lib/engine/indexer.js +242 -0
  75. package/lib/engine/parser.js +53 -0
  76. package/lib/engine/query-expander.js +42 -0
  77. package/lib/engine/reranker.js +40 -0
  78. package/lib/engine/rrf.js +59 -0
  79. package/lib/engine/scanner.js +151 -0
  80. package/lib/engine/searcher.js +139 -0
  81. package/lib/engine/session-coordinator.js +306 -0
  82. package/lib/engine/session-manager.js +429 -0
  83. package/lib/engine/synthesizer.js +70 -0
  84. package/lib/installer.js +70 -0
  85. package/lib/instinct-block.js +33 -0
  86. package/lib/mcp-config-writer.js +88 -0
  87. package/lib/paths.js +57 -0
  88. package/lib/profiles/design.md +19 -0
  89. package/lib/profiles/general.md +16 -0
  90. package/lib/profiles/research-analysis.md +22 -0
  91. package/lib/profiles/software-development.md +23 -0
  92. package/lib/profiles/writing-content.md +19 -0
  93. package/lib/project-initializer.js +916 -0
  94. package/lib/registry/skills.js +102 -0
  95. package/lib/registry-searcher.js +99 -0
  96. package/lib/rules/rules-manager.js +169 -0
  97. package/lib/skill-fetcher.js +333 -0
  98. package/lib/well-known-builder.js +70 -0
  99. package/lib/wizard/index.js +404 -0
  100. package/lib/wizard/integration-detector.js +41 -0
  101. package/lib/wizard/project-detector.js +100 -0
  102. package/lib/wizard/prompt.js +156 -0
  103. package/lib/wizard/registry-embeddings.js +107 -0
  104. package/lib/wizard/skill-recommender.js +69 -0
  105. package/llms-full.txt +254 -0
  106. package/llms.txt +70 -0
  107. package/package.json +45 -0
  108. package/research-reports/2026-04-01-current-architecture.md +160 -0
  109. package/research-reports/IDEAS.md +93 -0
  110. package/rules/common/clean-code.md +42 -0
  111. package/rules/java/effective-java.md +42 -0
  112. package/rules/kotlin/effective-kotlin.md +37 -0
  113. package/rules/python/effective-python.md +38 -0
  114. package/rules/rust/rust.md +37 -0
  115. package/rules/typescript/effective-typescript.md +42 -0
  116. package/scripts/gen-llms-full.mjs +36 -0
  117. package/scripts/gen-og.mjs +142 -0
  118. package/scripts/validate-frontmatter.js +25 -0
  119. package/skills/animation-at-work/SKILL.md +270 -0
  120. package/skills/animation-at-work/assets/example_asset.txt +1 -0
  121. package/skills/animation-at-work/evals/evals.json +44 -0
  122. package/skills/animation-at-work/evals/results.json +13 -0
  123. package/skills/animation-at-work/examples/after.md +64 -0
  124. package/skills/animation-at-work/examples/before.md +35 -0
  125. package/skills/animation-at-work/references/api_reference.md +369 -0
  126. package/skills/animation-at-work/references/review-checklist.md +79 -0
  127. package/skills/animation-at-work/scripts/audit_animations.py +295 -0
  128. package/skills/animation-at-work/scripts/example.py +1 -0
  129. package/skills/clean-code-reviewer/SKILL.md +444 -0
  130. package/skills/clean-code-reviewer/audit.json +35 -0
  131. package/skills/clean-code-reviewer/evals/evals.json +185 -0
  132. package/skills/clean-code-reviewer/evals/results.json +13 -0
  133. package/skills/clean-code-reviewer/examples/after.md +48 -0
  134. package/skills/clean-code-reviewer/examples/before.md +33 -0
  135. package/skills/clean-code-reviewer/references/api_reference.md +158 -0
  136. package/skills/clean-code-reviewer/references/practices-catalog.md +282 -0
  137. package/skills/clean-code-reviewer/references/review-checklist.md +254 -0
  138. package/skills/clean-code-reviewer/scripts/pre-review.py +206 -0
  139. package/skills/data-intensive-patterns/SKILL.md +267 -0
  140. package/skills/data-intensive-patterns/assets/example_asset.txt +1 -0
  141. package/skills/data-intensive-patterns/evals/evals.json +54 -0
  142. package/skills/data-intensive-patterns/evals/results.json +13 -0
  143. package/skills/data-intensive-patterns/examples/after.md +61 -0
  144. package/skills/data-intensive-patterns/examples/before.md +38 -0
  145. package/skills/data-intensive-patterns/references/api_reference.md +34 -0
  146. package/skills/data-intensive-patterns/references/patterns-catalog.md +551 -0
  147. package/skills/data-intensive-patterns/references/review-checklist.md +193 -0
  148. package/skills/data-intensive-patterns/scripts/adr.py +213 -0
  149. package/skills/data-intensive-patterns/scripts/example.py +1 -0
  150. package/skills/data-pipelines/SKILL.md +259 -0
  151. package/skills/data-pipelines/assets/example_asset.txt +1 -0
  152. package/skills/data-pipelines/evals/evals.json +45 -0
  153. package/skills/data-pipelines/evals/results.json +13 -0
  154. package/skills/data-pipelines/examples/after.md +97 -0
  155. package/skills/data-pipelines/examples/before.md +37 -0
  156. package/skills/data-pipelines/references/api_reference.md +301 -0
  157. package/skills/data-pipelines/references/review-checklist.md +181 -0
  158. package/skills/data-pipelines/scripts/example.py +1 -0
  159. package/skills/data-pipelines/scripts/new_pipeline.py +444 -0
  160. package/skills/design-patterns/SKILL.md +271 -0
  161. package/skills/design-patterns/assets/example_asset.txt +1 -0
  162. package/skills/design-patterns/evals/evals.json +46 -0
  163. package/skills/design-patterns/evals/results.json +13 -0
  164. package/skills/design-patterns/examples/after.md +52 -0
  165. package/skills/design-patterns/examples/before.md +29 -0
  166. package/skills/design-patterns/references/api_reference.md +1 -0
  167. package/skills/design-patterns/references/patterns-catalog.md +726 -0
  168. package/skills/design-patterns/references/review-checklist.md +173 -0
  169. package/skills/design-patterns/scripts/example.py +1 -0
  170. package/skills/design-patterns/scripts/scaffold.py +807 -0
  171. package/skills/domain-driven-design/SKILL.md +142 -0
  172. package/skills/domain-driven-design/assets/example_asset.txt +1 -0
  173. package/skills/domain-driven-design/evals/evals.json +48 -0
  174. package/skills/domain-driven-design/evals/results.json +13 -0
  175. package/skills/domain-driven-design/examples/after.md +80 -0
  176. package/skills/domain-driven-design/examples/before.md +43 -0
  177. package/skills/domain-driven-design/references/api_reference.md +1 -0
  178. package/skills/domain-driven-design/references/patterns-catalog.md +545 -0
  179. package/skills/domain-driven-design/references/review-checklist.md +158 -0
  180. package/skills/domain-driven-design/scripts/example.py +1 -0
  181. package/skills/domain-driven-design/scripts/scaffold.py +421 -0
  182. package/skills/effective-java/SKILL.md +227 -0
  183. package/skills/effective-java/assets/example_asset.txt +1 -0
  184. package/skills/effective-java/evals/evals.json +46 -0
  185. package/skills/effective-java/evals/results.json +13 -0
  186. package/skills/effective-java/examples/after.md +83 -0
  187. package/skills/effective-java/examples/before.md +37 -0
  188. package/skills/effective-java/references/api_reference.md +1 -0
  189. package/skills/effective-java/references/items-catalog.md +955 -0
  190. package/skills/effective-java/references/review-checklist.md +216 -0
  191. package/skills/effective-java/scripts/checkstyle_setup.py +211 -0
  192. package/skills/effective-java/scripts/example.py +1 -0
  193. package/skills/effective-kotlin/SKILL.md +271 -0
  194. package/skills/effective-kotlin/assets/example_asset.txt +1 -0
  195. package/skills/effective-kotlin/audit.json +29 -0
  196. package/skills/effective-kotlin/evals/evals.json +45 -0
  197. package/skills/effective-kotlin/evals/results.json +13 -0
  198. package/skills/effective-kotlin/examples/after.md +36 -0
  199. package/skills/effective-kotlin/examples/before.md +38 -0
  200. package/skills/effective-kotlin/references/api_reference.md +1 -0
  201. package/skills/effective-kotlin/references/practices-catalog.md +1228 -0
  202. package/skills/effective-kotlin/references/review-checklist.md +126 -0
  203. package/skills/effective-kotlin/scripts/example.py +1 -0
  204. package/skills/effective-python/SKILL.md +441 -0
  205. package/skills/effective-python/evals/evals.json +44 -0
  206. package/skills/effective-python/evals/results.json +13 -0
  207. package/skills/effective-python/examples/after.md +56 -0
  208. package/skills/effective-python/examples/before.md +40 -0
  209. package/skills/effective-python/ref-01-pythonic-thinking.md +202 -0
  210. package/skills/effective-python/ref-02-lists-and-dicts.md +146 -0
  211. package/skills/effective-python/ref-03-functions.md +186 -0
  212. package/skills/effective-python/ref-04-comprehensions-generators.md +211 -0
  213. package/skills/effective-python/ref-05-classes-interfaces.md +188 -0
  214. package/skills/effective-python/ref-06-metaclasses-attributes.md +209 -0
  215. package/skills/effective-python/ref-07-concurrency.md +213 -0
  216. package/skills/effective-python/ref-08-robustness-performance.md +248 -0
  217. package/skills/effective-python/ref-09-testing-debugging.md +253 -0
  218. package/skills/effective-python/ref-10-collaboration.md +175 -0
  219. package/skills/effective-python/references/api_reference.md +218 -0
  220. package/skills/effective-python/references/practices-catalog.md +483 -0
  221. package/skills/effective-python/references/review-checklist.md +190 -0
  222. package/skills/effective-python/scripts/lint.py +173 -0
  223. package/skills/effective-typescript/SKILL.md +262 -0
  224. package/skills/effective-typescript/audit.json +29 -0
  225. package/skills/effective-typescript/evals/evals.json +37 -0
  226. package/skills/effective-typescript/evals/results.json +13 -0
  227. package/skills/effective-typescript/examples/after.md +70 -0
  228. package/skills/effective-typescript/examples/before.md +47 -0
  229. package/skills/effective-typescript/references/api_reference.md +118 -0
  230. package/skills/effective-typescript/references/practices-catalog.md +371 -0
  231. package/skills/effective-typescript/scripts/review.py +169 -0
  232. package/skills/kotlin-in-action/SKILL.md +261 -0
  233. package/skills/kotlin-in-action/assets/example_asset.txt +1 -0
  234. package/skills/kotlin-in-action/evals/evals.json +43 -0
  235. package/skills/kotlin-in-action/evals/results.json +13 -0
  236. package/skills/kotlin-in-action/examples/after.md +53 -0
  237. package/skills/kotlin-in-action/examples/before.md +39 -0
  238. package/skills/kotlin-in-action/references/api_reference.md +1 -0
  239. package/skills/kotlin-in-action/references/practices-catalog.md +436 -0
  240. package/skills/kotlin-in-action/references/review-checklist.md +204 -0
  241. package/skills/kotlin-in-action/scripts/example.py +1 -0
  242. package/skills/kotlin-in-action/scripts/setup_detekt.py +224 -0
  243. package/skills/lean-startup/SKILL.md +160 -0
  244. package/skills/lean-startup/assets/example_asset.txt +1 -0
  245. package/skills/lean-startup/evals/evals.json +43 -0
  246. package/skills/lean-startup/evals/results.json +13 -0
  247. package/skills/lean-startup/examples/after.md +80 -0
  248. package/skills/lean-startup/examples/before.md +34 -0
  249. package/skills/lean-startup/references/api_reference.md +319 -0
  250. package/skills/lean-startup/references/review-checklist.md +137 -0
  251. package/skills/lean-startup/scripts/example.py +1 -0
  252. package/skills/lean-startup/scripts/new_experiment.py +286 -0
  253. package/skills/microservices-patterns/SKILL.md +384 -0
  254. package/skills/microservices-patterns/evals/evals.json +45 -0
  255. package/skills/microservices-patterns/evals/results.json +13 -0
  256. package/skills/microservices-patterns/examples/after.md +69 -0
  257. package/skills/microservices-patterns/examples/before.md +40 -0
  258. package/skills/microservices-patterns/references/patterns-catalog.md +391 -0
  259. package/skills/microservices-patterns/references/review-checklist.md +169 -0
  260. package/skills/microservices-patterns/scripts/new_service.py +583 -0
  261. package/skills/programming-with-rust/SKILL.md +209 -0
  262. package/skills/programming-with-rust/evals/evals.json +37 -0
  263. package/skills/programming-with-rust/evals/results.json +13 -0
  264. package/skills/programming-with-rust/examples/after.md +107 -0
  265. package/skills/programming-with-rust/examples/before.md +59 -0
  266. package/skills/programming-with-rust/references/api_reference.md +152 -0
  267. package/skills/programming-with-rust/references/practices-catalog.md +335 -0
  268. package/skills/programming-with-rust/scripts/review.py +142 -0
  269. package/skills/refactoring-ui/SKILL.md +362 -0
  270. package/skills/refactoring-ui/assets/example_asset.txt +1 -0
  271. package/skills/refactoring-ui/evals/evals.json +45 -0
  272. package/skills/refactoring-ui/evals/results.json +13 -0
  273. package/skills/refactoring-ui/examples/after.md +85 -0
  274. package/skills/refactoring-ui/examples/before.md +58 -0
  275. package/skills/refactoring-ui/references/api_reference.md +355 -0
  276. package/skills/refactoring-ui/references/review-checklist.md +114 -0
  277. package/skills/refactoring-ui/scripts/audit_css.py +250 -0
  278. package/skills/refactoring-ui/scripts/example.py +1 -0
  279. package/skills/rust-in-action/SKILL.md +350 -0
  280. package/skills/rust-in-action/evals/evals.json +38 -0
  281. package/skills/rust-in-action/evals/results.json +13 -0
  282. package/skills/rust-in-action/examples/after.md +156 -0
  283. package/skills/rust-in-action/examples/before.md +56 -0
  284. package/skills/rust-in-action/references/practices-catalog.md +346 -0
  285. package/skills/rust-in-action/scripts/review.py +147 -0
  286. package/skills/skill-router/SKILL.md +186 -0
  287. package/skills/skill-router/evals/evals.json +38 -0
  288. package/skills/skill-router/evals/results.json +13 -0
  289. package/skills/skill-router/examples/after.md +63 -0
  290. package/skills/skill-router/examples/before.md +39 -0
  291. package/skills/skill-router/references/api_reference.md +24 -0
  292. package/skills/skill-router/references/routing-heuristics.md +89 -0
  293. package/skills/skill-router/references/skill-catalog.md +174 -0
  294. package/skills/skill-router/scripts/route.py +266 -0
  295. package/skills/spring-boot-in-action/SKILL.md +340 -0
  296. package/skills/spring-boot-in-action/evals/evals.json +39 -0
  297. package/skills/spring-boot-in-action/evals/results.json +13 -0
  298. package/skills/spring-boot-in-action/examples/after.md +185 -0
  299. package/skills/spring-boot-in-action/examples/before.md +84 -0
  300. package/skills/spring-boot-in-action/references/practices-catalog.md +403 -0
  301. package/skills/spring-boot-in-action/scripts/review.py +184 -0
  302. package/skills/storytelling-with-data/SKILL.md +241 -0
  303. package/skills/storytelling-with-data/assets/example_asset.txt +1 -0
  304. package/skills/storytelling-with-data/evals/evals.json +47 -0
  305. package/skills/storytelling-with-data/evals/results.json +13 -0
  306. package/skills/storytelling-with-data/examples/after.md +50 -0
  307. package/skills/storytelling-with-data/examples/before.md +33 -0
  308. package/skills/storytelling-with-data/references/api_reference.md +379 -0
  309. package/skills/storytelling-with-data/references/review-checklist.md +111 -0
  310. package/skills/storytelling-with-data/scripts/chart_review.py +301 -0
  311. package/skills/storytelling-with-data/scripts/example.py +1 -0
  312. package/skills/system-design-interview/SKILL.md +233 -0
  313. package/skills/system-design-interview/assets/example_asset.txt +1 -0
  314. package/skills/system-design-interview/evals/evals.json +46 -0
  315. package/skills/system-design-interview/evals/results.json +13 -0
  316. package/skills/system-design-interview/examples/after.md +94 -0
  317. package/skills/system-design-interview/examples/before.md +27 -0
  318. package/skills/system-design-interview/references/api_reference.md +582 -0
  319. package/skills/system-design-interview/references/review-checklist.md +201 -0
  320. package/skills/system-design-interview/scripts/example.py +1 -0
  321. package/skills/system-design-interview/scripts/new_design.py +421 -0
  322. package/skills/using-asyncio-python/SKILL.md +290 -0
  323. package/skills/using-asyncio-python/assets/example_asset.txt +1 -0
  324. package/skills/using-asyncio-python/evals/evals.json +43 -0
  325. package/skills/using-asyncio-python/evals/results.json +13 -0
  326. package/skills/using-asyncio-python/examples/after.md +68 -0
  327. package/skills/using-asyncio-python/examples/before.md +39 -0
  328. package/skills/using-asyncio-python/references/api_reference.md +267 -0
  329. package/skills/using-asyncio-python/references/review-checklist.md +149 -0
  330. package/skills/using-asyncio-python/scripts/check_blocking.py +270 -0
  331. package/skills/using-asyncio-python/scripts/example.py +1 -0
  332. package/skills/web-scraping-python/SKILL.md +280 -0
  333. package/skills/web-scraping-python/assets/example_asset.txt +1 -0
  334. package/skills/web-scraping-python/evals/evals.json +46 -0
  335. package/skills/web-scraping-python/evals/results.json +13 -0
  336. package/skills/web-scraping-python/examples/after.md +109 -0
  337. package/skills/web-scraping-python/examples/before.md +40 -0
  338. package/skills/web-scraping-python/references/api_reference.md +393 -0
  339. package/skills/web-scraping-python/references/review-checklist.md +163 -0
  340. package/skills/web-scraping-python/scripts/example.py +1 -0
  341. package/skills/web-scraping-python/scripts/new_scraper.py +231 -0
  342. package/skills/writing-plans/audit.json +34 -0
  343. package/tests/agent-detector.test.js +83 -0
  344. package/tests/corrections.test.js +245 -0
  345. package/tests/doctor/hook-installer.test.js +72 -0
  346. package/tests/doctor/usage-tracker.test.js +140 -0
  347. package/tests/engine/benchmark-eval.test.js +31 -0
  348. package/tests/engine/bm25-index.test.js +85 -0
  349. package/tests/engine/capture-command.test.js +35 -0
  350. package/tests/engine/capture.test.js +17 -0
  351. package/tests/engine/graph-augmented-search.test.js +107 -0
  352. package/tests/engine/graph-injector.test.js +44 -0
  353. package/tests/engine/graph.test.js +216 -0
  354. package/tests/engine/hybrid-searcher.test.js +74 -0
  355. package/tests/engine/indexer-bm25.test.js +37 -0
  356. package/tests/engine/mcp-tools.test.js +73 -0
  357. package/tests/engine/project-initializer-mcp.test.js +99 -0
  358. package/tests/engine/query-expander.test.js +36 -0
  359. package/tests/engine/reranker.test.js +51 -0
  360. package/tests/engine/rrf.test.js +49 -0
  361. package/tests/engine/srag-prefix.test.js +47 -0
  362. package/tests/instinct-block.test.js +23 -0
  363. package/tests/mcp-config-writer.test.js +60 -0
  364. package/tests/project-initializer-new-agents.test.js +48 -0
  365. package/tests/rules/rules-manager.test.js +230 -0
  366. package/tests/well-known-builder.test.js +40 -0
  367. package/tests/wizard/integration-detector.test.js +31 -0
  368. package/tests/wizard/project-detector.test.js +51 -0
  369. package/tests/wizard/prompt-session.test.js +61 -0
  370. package/tests/wizard/prompt.test.js +16 -0
  371. package/tests/wizard/registry-embeddings.test.js +35 -0
  372. package/tests/wizard/skill-recommender.test.js +34 -0
  373. package/tests/wizard/slot-count.test.js +25 -0
  374. package/vercel.json +21 -0
@@ -0,0 +1,271 @@
1
+ ---
2
+ name: design-patterns
3
+ version: "1.0"
4
+ license: MIT
5
+ tags: [patterns, oop, architecture]
6
+ description: >
7
+ Apply and review GoF design patterns from Head First Design Patterns. Use for
8
+ Creational patterns (Factory Method, Abstract Factory, Singleton, Builder,
9
+ Prototype), Structural patterns (Adapter, Bridge, Composite, Decorator, Facade,
10
+ Flyweight, Proxy), Behavioral patterns (Chain of Responsibility, Command,
11
+ Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template
12
+ Method, Visitor), compound patterns (MVC), and OO design principles. Trigger on
13
+ "design pattern", "GoF", "Gang of Four", "factory", "singleton", "observer",
14
+ "strategy", "decorator", "adapter", "facade", "proxy", "composite", "command",
15
+ "iterator", "state", "template method", "builder", "prototype", "bridge",
16
+ "flyweight", "mediator", "memento", "visitor", "chain of responsibility",
17
+ "interpreter", "MVC", "refactor to pattern", or "code smells."
18
+ ---
19
+
20
+ # Design Patterns Skill
21
+
22
+ You are an expert software designer grounded in the 23 Gang of Four design patterns
23
+ as taught in *Head First Design Patterns* by Eric Freeman & Elisabeth Robson. You
24
+ help developers in two modes:
25
+
26
+ 1. **Code Generation** — Produce well-structured code that applies the right pattern(s)
27
+ 2. **Code Review** — Analyze existing code and recommend pattern-based improvements
28
+
29
+ ## How to Decide Which Mode
30
+
31
+ - If the user asks you to *build*, *create*, *generate*, *implement*, *design*, or *refactor* something → **Code Generation**
32
+ - If the user asks you to *review*, *check*, *improve*, *audit*, *critique*, or *identify patterns* in code → **Code Review**
33
+ - If ambiguous, ask briefly which mode they'd prefer
34
+
35
+ ---
36
+
37
+ ## Mode 1: Code Generation
38
+
39
+ When generating code using design patterns, follow this decision flow:
40
+
41
+ ### Step 1 — Understand the Design Problem
42
+
43
+ Ask (or infer from context) what the design needs:
44
+
45
+ - **What varies?** — Identify the aspects that change so you can encapsulate them
46
+ - **What's rigid?** — Find tightly coupled code or areas that resist change
47
+ - **What are the forces?** — Flexibility, extensibility, testability, simplicity?
48
+ - **Language/framework** — What language and constraints apply?
49
+
50
+ ### Step 2 — Select the Right Pattern
51
+
52
+ Read `references/patterns-catalog.md` for full pattern details. Quick decision guide:
53
+
54
+ | Design Problem | Patterns to Consider |
55
+ |----------------|---------------------|
56
+ | Algorithm or behavior varies at runtime | **Strategy** (Context stores a strategy reference as a field; behavior swapped by setting a different strategy object) |
57
+ | Objects need to be notified of state changes | **Observer** (subject maintains subscriber list, push/pull notification) |
58
+ | Add responsibilities dynamically without subclassing | **Decorator** (wrap objects with additional behavior, same interface) |
59
+ | Object creation varies or is complex | **Factory Method** (subclass decides), **Abstract Factory** (families of related objects), **Builder** (step-by-step construction) |
60
+ | Ensure only one instance exists globally | **Singleton** (private constructor, thread-safe access) |
61
+ | Encapsulate requests as objects for undo/queue/log | **Command** (receiver, command, invoker; supports undo/redo, macro commands) |
62
+ | Convert an incompatible interface | **Adapter** (wrap adaptee, translate interface calls) |
63
+ | Simplify a complex subsystem interface | **Facade** (unified high-level interface, reduce coupling) |
64
+ | Define algorithm skeleton, let subclasses fill steps | **Template Method** (abstract base with hooks, Hollywood Principle) |
65
+ | Traverse a collection without exposing internals | **Iterator** (uniform traversal, Single Responsibility Principle) |
66
+ | Treat individual objects and compositions uniformly | **Composite** (tree structure, component/leaf/composite roles) |
67
+ | Object behavior changes based on internal state | **State** (delegate to state objects, eliminate conditionals) |
68
+ | Control access to an object | **Proxy** (remote, virtual, protection proxy patterns) |
69
+ | Decouple abstraction from implementation | **Bridge** (two hierarchies vary independently) |
70
+ | Share common state across many objects | **Flyweight** (intrinsic vs extrinsic state, factory-managed pool) |
71
+ | Pass request along a chain of potential handlers | **Chain of Responsibility** (decouple sender and receiver) |
72
+ | Build interpreter for a simple language/grammar | **Interpreter** (grammar rules as classes, recursive evaluation) |
73
+ | Centralize complex inter-object communication | **Mediator** (objects communicate through mediator, not directly) |
74
+ | Capture and restore object state without violating encapsulation | **Memento** (originator creates, caretaker stores) |
75
+ | Create objects by cloning existing instances | **Prototype** (clone from registry, avoid costly construction) |
76
+ | Add operations to class structures without modifying them | **Visitor** (double dispatch, new operations without changing element classes) |
77
+ | Combine multiple patterns for rich architecture | **MVC** (Strategy + Observer + Composite), **Compound Patterns** |
78
+
79
+ ### Step 3 — Apply OO Design Principles
80
+
81
+ Every pattern application should honor these principles:
82
+
83
+ 1. **Encapsulate what varies** — Identify parts that change and separate them from what stays the same
84
+ 2. **Favor composition over inheritance** — HAS-A is more flexible than IS-A
85
+ 3. **Program to interfaces, not implementations** — Depend on abstractions
86
+ 4. **Strive for loosely coupled designs** — Minimize interdependencies between objects
87
+ 5. **Open-Closed Principle** — Open for extension, closed for modification
88
+ 6. **Dependency Inversion Principle** — Depend on abstractions, not concretions
89
+ 7. **Principle of Least Knowledge (Law of Demeter)** — Only talk to immediate friends
90
+ 8. **Hollywood Principle** — Don't call us, we'll call you (high-level components control flow)
91
+ 9. **Single Responsibility Principle** — One reason to change per class
92
+
93
+ ### Step 4 — Generate the Code
94
+
95
+ Follow these guidelines when writing pattern-based code:
96
+
97
+ - **Name classes after pattern roles** — Use pattern vocabulary: Subject/Observer, Strategy/Context, Command/Invoker/Receiver, Component/Decorator, Factory, etc.
98
+ - **Show the pattern structure clearly** — Interface/abstract class first, then concrete implementations, then client code
99
+ - **Include usage example** — Show how client code uses the pattern
100
+ - **Document which pattern** — Comment at the top which pattern(s) are being applied and why
101
+ - **Keep it practical** — Don't over-engineer; apply patterns only where they solve a real problem
102
+ - **Compose patterns when appropriate** — Real designs often combine patterns (e.g., MVC = Strategy + Observer + Composite)
103
+
104
+ When generating code, produce:
105
+
106
+ 1. **Pattern identification** — Which pattern(s) and why
107
+ 2. **Interface/abstract definitions** — The contracts
108
+ 3. **Concrete implementations** — The participating classes
109
+ 4. **Client/usage code** — How it all connects
110
+ 5. **Extension example** — Show how the design is easy to extend
111
+
112
+ ### Code Generation Examples
113
+
114
+ **Example 1 — Strategy Pattern:**
115
+ ```
116
+ User: "I have a duck simulator where different duck types fly and quack
117
+ differently, and I need to add/change behaviors at runtime"
118
+
119
+ You should generate:
120
+ - FlyBehavior interface with fly() method
121
+ - Concrete: FlyWithWings, FlyNoWay, FlyRocketPowered
122
+ - QuackBehavior interface with quack() method
123
+ - Concrete: Quack, Squeak, MuteQuack
124
+ - Duck abstract class composing FlyBehavior + QuackBehavior
125
+ (fields: private FlyBehavior flyBehavior; private QuackBehavior quackBehavior)
126
+ (Context holds a persistent reference to the strategy, not a per-call lookup)
127
+ - Concrete ducks: MallardDuck, RubberDuck, DecoyDuck
128
+ - Setter methods for runtime behavior change (setFlyBehavior / setQuackBehavior)
129
+ ```
130
+
131
+ **Key Strategy rule:** The Context class MUST hold a strategy reference as a stored field
132
+ (not look it up in a map or create it on each call). The client injects a strategy object
133
+ into the context (via constructor or setter), and the context delegates to
134
+ `strategy.doSomething()` on every operation. This is what allows the behavior to be
135
+ swapped at runtime without changing the context class.
136
+
137
+ **Example 2 — Decorator Pattern:**
138
+ ```
139
+ User: "Coffee shop ordering system where beverages can have any
140
+ combination of add-ons, each affecting cost and description"
141
+
142
+ You should generate:
143
+ - Beverage abstract component (getDescription(), cost())
144
+ - Concrete beverages: HouseBlend, DarkRoast, Espresso
145
+ - CondimentDecorator abstract class extends Beverage
146
+ - Concrete decorators: Mocha, Whip, Soy, SteamedMilk
147
+ - Each decorator wraps a Beverage, delegates + adds behavior
148
+ - Client code showing composition: new Mocha(new Whip(new DarkRoast()))
149
+ ```
150
+
151
+ **Example 3 — State Pattern:**
152
+ ```
153
+ User: "Gumball machine with states: no quarter, has quarter,
154
+ sold, out of gumballs — with state-specific behavior"
155
+
156
+ You should generate:
157
+ - State interface: insertQuarter(), ejectQuarter(), turnCrank(), dispense()
158
+ - Concrete states: NoQuarterState, HasQuarterState, SoldState, SoldOutState
159
+ - GumballMachine context holds current State, delegates all actions
160
+ - State transitions managed by state objects calling machine.setState()
161
+ - Each state handles all actions appropriately for its context
162
+ ```
163
+
164
+ **Example 4 — Compound Pattern (MVC):**
165
+ ```
166
+ User: "Build a beat controller with model-view-controller separation"
167
+
168
+ You should generate:
169
+ - Model (Observable): BeatModel with BPM state, registers observers
170
+ - View (Composite/Observer): DJView observes model, displays BPM and controls
171
+ - Controller (Strategy): BeatController implements strategy for view
172
+ - View delegates user actions to controller
173
+ - Model notifies view of state changes
174
+ - Controller mediates between view and model
175
+ ```
176
+
177
+ ---
178
+
179
+ ## Mode 2: Code Review
180
+
181
+ When reviewing code for design pattern opportunities and correctness, read
182
+ `references/review-checklist.md` for the full checklist. Apply these categories:
183
+
184
+ ### Review Process
185
+
186
+ 1. **Identify existing patterns** — What patterns are already in use (explicitly or accidentally)?
187
+ 2. **Check pattern correctness** — Are the patterns applied properly with all participants?
188
+ 3. **Find pattern opportunities** — Where could patterns reduce complexity?
189
+ 4. **Evaluate OO principles** — Are the nine design principles being honored?
190
+ 5. **Spot anti-patterns and code smells** — What structural problems exist?
191
+ 6. **Assess composition vs inheritance** — Is inheritance overused where composition would be better?
192
+
193
+ ### Reviewing Correct or Well-Implemented Code
194
+
195
+ When code already applies a pattern correctly, **lead with explicit recognition and praise
196
+ of what it does right.** Do NOT invent problems to seem thorough. Specifically:
197
+
198
+ - State upfront that this is a correct/well-implemented pattern
199
+ - Praise each element that specifically avoids a known pitfall (e.g., `removeObserver`
200
+ prevents memory leaks; defensive copy in `notifyObservers` prevents
201
+ ConcurrentModificationException; interface-based Observer prevents coupling)
202
+ - Any improvements (thread safety, alternative APIs, etc.) MUST be clearly labeled
203
+ "optional improvement" or "non-critical suggestion" — never frame them as bugs or
204
+ required fixes unless the code is actually incorrect
205
+ - Do NOT raise design extensibility concerns (e.g., "what if requirements change") as
206
+ present problems — the task is to review the code as written, not imagine future needs
207
+
208
+ ### Review Output Format
209
+
210
+ Structure your review as:
211
+
212
+ ```
213
+ ## Summary
214
+ One paragraph: patterns identified, overall design quality, principle adherence.
215
+
216
+ ## Patterns Found
217
+ For each pattern found:
218
+ - **Pattern**: name and classification (Creational/Structural/Behavioral)
219
+ - **Implementation quality**: correct/partially correct/incorrect
220
+ - **Issues**: any problems with the implementation
221
+
222
+ ## Pattern Opportunities
223
+ For each opportunity:
224
+ - **Problem**: the code smell or design issue
225
+ - **Suggested pattern**: which pattern(s) would help
226
+ - **Benefit**: what improves (flexibility, testability, etc.)
227
+ - **Sketch**: brief code outline of the improvement
228
+
229
+ ## Principle Violations
230
+ Which of the nine OO principles are being violated and where.
231
+
232
+ ## Recommendations
233
+ Priority-ordered list from most critical to nice-to-have.
234
+ ```
235
+
236
+ ### Common Anti-Patterns and Code Smells to Flag
237
+
238
+ - **Conditional complexity** — Large switch/if-else chains that select behavior → Strategy or State pattern. In the Strategy refactor, the Context must store the strategy as a field (not look it up on each call); per-call factory creation defeats the ability to swap behavior at runtime
239
+ - **Rigid class hierarchies** — Deep inheritance trees with overridden methods → Composition + Strategy/Decorator
240
+ - **Duplicated code across subclasses** — Same algorithm with varying steps → Template Method
241
+ - **Tight coupling to concrete classes** — Client code creates specific classes → Factory patterns
242
+ - **God class** — One class doing too much → Extract responsibilities using SRP + patterns
243
+ - **Primitive obsession** — Using primitives where objects with behavior are needed
244
+ - **Feature envy** — Methods that use another class's data more than their own
245
+ - **Exposed collection internals** — Returning mutable internal collections → Iterator
246
+ - **Missing encapsulation of what varies** — Hardcoded behavior that should be configurable → Strategy
247
+ - **Inheritance for code reuse only** — Using IS-A when HAS-A is appropriate → Composition
248
+ - **Violated Law of Demeter** — Method chains like a.getB().getC().doThing() → Facade or method delegation
249
+ - **Observer memory leaks** — Registered observers never unregistered (if `removeObserver` IS present, explicitly praise it as addressing this pitfall)
250
+ - **Singleton abuse** — Using Singleton as a global variable container rather than for genuine single-instance needs
251
+ - **Empty or trivial pattern implementations** — Pattern skeleton without real purpose (pattern for pattern's sake)
252
+ - **Incomplete pattern** — Missing participants (Command without undo, Observer without unsubscribe)
253
+
254
+ ---
255
+
256
+ ## General Guidelines
257
+
258
+ - Be practical, not dogmatic. Patterns solve specific design problems — don't force
259
+ them where simpler code works fine. "The simplest thing that works" is often right.
260
+ - The core goal is **managing change** — patterns make software easier to extend and
261
+ modify without breaking existing code.
262
+ - **Encapsulate what varies** is the most fundamental principle. Start every design
263
+ analysis by identifying what changes.
264
+ - **Favor composition over inheritance** is the second most important principle.
265
+ Most patterns use composition to achieve flexibility.
266
+ - Patterns are often combined in real systems. MVC alone uses three patterns.
267
+ Don't think in single-pattern terms.
268
+ - Know when NOT to use a pattern. Over-engineering with patterns is as bad as not
269
+ using them. Apply when there's a demonstrated need.
270
+ - For deeper pattern details, read `references/patterns-catalog.md` before generating code.
271
+ - For review checklists, read `references/review-checklist.md` before reviewing code.
@@ -0,0 +1,46 @@
1
+ {
2
+ "evals": [
3
+ {
4
+ "id": "eval-01-strategy-pattern-if-else",
5
+ "prompt": "Review this Java code:\n\n```java\npublic class PaymentProcessor {\n public void processPayment(String paymentType, double amount) {\n if (paymentType.equals(\"CREDIT_CARD\")) {\n System.out.println(\"Validating credit card...\");\n System.out.println(\"Charging $\" + amount + \" to credit card\");\n System.out.println(\"Sending credit card receipt\");\n } else if (paymentType.equals(\"PAYPAL\")) {\n System.out.println(\"Redirecting to PayPal...\");\n System.out.println(\"Confirming PayPal payment of $\" + amount);\n System.out.println(\"Sending PayPal confirmation email\");\n } else if (paymentType.equals(\"CRYPTO\")) {\n System.out.println(\"Opening crypto wallet...\");\n System.out.println(\"Broadcasting transaction of $\" + amount);\n System.out.println(\"Waiting for blockchain confirmation\");\n } else if (paymentType.equals(\"BANK_TRANSFER\")) {\n System.out.println(\"Initiating bank transfer...\");\n System.out.println(\"Transferring $\" + amount + \" via ACH\");\n System.out.println(\"Sending bank transfer confirmation\");\n } else {\n throw new IllegalArgumentException(\"Unknown payment type: \" + paymentType);\n }\n }\n \n public double calculateFee(String paymentType, double amount) {\n if (paymentType.equals(\"CREDIT_CARD\")) {\n return amount * 0.029 + 0.30;\n } else if (paymentType.equals(\"PAYPAL\")) {\n return amount * 0.034 + 0.30;\n } else if (paymentType.equals(\"CRYPTO\")) {\n return amount * 0.01;\n } else if (paymentType.equals(\"BANK_TRANSFER\")) {\n return 0.25;\n } else {\n throw new IllegalArgumentException(\"Unknown payment type: \" + paymentType);\n }\n }\n}\n```",
6
+ "expectations": [
7
+ "Identifies this as a prime Strategy pattern candidate — the payment algorithm varies by type",
8
+ "Calls out the Open-Closed Principle violation: adding a new payment method requires modifying this class",
9
+ "Notes that the parallel if/else chains in processPayment and calculateFee are a code smell (duplicated conditional logic)",
10
+ "Recommends extracting a PaymentStrategy interface with processPayment(double amount) and calculateFee(double amount) methods",
11
+ "Suggests concrete strategy classes: CreditCardStrategy, PayPalStrategy, CryptoStrategy, BankTransferStrategy",
12
+ "Describes the Context class (PaymentProcessor) holding a reference to the strategy interface",
13
+ "Notes that using a String 'paymentType' is fragile — a Map<String, PaymentStrategy> or enum-based lookup is safer",
14
+ "Provides a sketch of the refactored interface and at least one concrete implementation",
15
+ "References the principle: 'Encapsulate what varies' — payment behavior is what varies here"
16
+ ]
17
+ },
18
+ {
19
+ "id": "eval-02-unnecessary-singleton",
20
+ "prompt": "Review this Java code:\n\n```java\npublic class TaxCalculator {\n private static TaxCalculator instance;\n private double vatRate = 0.20;\n private double salesTaxRate = 0.08;\n \n private TaxCalculator() {}\n \n public static TaxCalculator getInstance() {\n if (instance == null) {\n instance = new TaxCalculator();\n }\n return instance;\n }\n \n public double calculateVAT(double price) {\n return price * vatRate;\n }\n \n public double calculateSalesTax(double price) {\n return price * salesTaxRate;\n }\n \n public void setVatRate(double vatRate) {\n this.vatRate = vatRate;\n }\n \n public void setSalesTaxRate(double salesTaxRate) {\n this.salesTaxRate = salesTaxRate;\n }\n}\n\n// Usage in application code:\nTaxCalculator calc = TaxCalculator.getInstance();\ncalc.setVatRate(0.23); // Portuguese VAT\nOrderTotal total = calc.calculateVAT(orderAmount);\n```",
21
+ "expectations": [
22
+ "Identifies the Singleton pattern and flags it as misapplied here",
23
+ "Explains why global mutable state is dangerous: setVatRate() and setSalesTaxRate() make this Singleton a shared mutable object — one caller's rate change affects all other callers",
24
+ "Notes the race condition: getInstance() is not thread-safe (no synchronization, no double-checked locking, no holder idiom)",
25
+ "Points out that a stateless or value-parameterized object has no reason to be a Singleton — TaxCalculator is just computation",
26
+ "Recommends removing the Singleton and using simple instantiation or dependency injection",
27
+ "Suggests making TaxCalculator immutable: accept rates in the constructor, remove setters",
28
+ "May suggest using the Strategy pattern if tax rules vary by jurisdiction",
29
+ "References the anti-pattern: 'Singleton abuse — using Singleton as a global variable container rather than for genuine single-instance needs'"
30
+ ]
31
+ },
32
+ {
33
+ "id": "eval-03-observer-pattern-correct",
34
+ "prompt": "Review this Java code:\n\n```java\npublic interface StockObserver {\n void update(String ticker, double price, double changePercent);\n}\n\npublic class StockMarket {\n private final Map<String, List<StockObserver>> observers = new HashMap<>();\n private final Map<String, Double> prices = new HashMap<>();\n \n public void registerObserver(String ticker, StockObserver observer) {\n observers.computeIfAbsent(ticker, k -> new ArrayList<>()).add(observer);\n }\n \n public void removeObserver(String ticker, StockObserver observer) {\n List<StockObserver> tickerObservers = observers.get(ticker);\n if (tickerObservers != null) {\n tickerObservers.remove(observer);\n }\n }\n \n public void updatePrice(String ticker, double newPrice) {\n double oldPrice = prices.getOrDefault(ticker, newPrice);\n prices.put(ticker, newPrice);\n double changePercent = oldPrice != 0 ? ((newPrice - oldPrice) / oldPrice) * 100 : 0;\n notifyObservers(ticker, newPrice, changePercent);\n }\n \n private void notifyObservers(String ticker, double price, double changePercent) {\n List<StockObserver> tickerObservers = observers.getOrDefault(ticker, Collections.emptyList());\n for (StockObserver observer : new ArrayList<>(tickerObservers)) {\n observer.update(ticker, price, changePercent);\n }\n }\n}\n\npublic class PriceAlertService implements StockObserver {\n private final double threshold;\n \n public PriceAlertService(double threshold) {\n this.threshold = threshold;\n }\n \n @Override\n public void update(String ticker, double price, double changePercent) {\n if (Math.abs(changePercent) >= threshold) {\n System.out.println(\"ALERT: \" + ticker + \" moved \" + changePercent + \"% to $\" + price);\n }\n }\n}\n```",
35
+ "expectations": [
36
+ "Recognizes this as a correctly implemented Observer pattern and says so explicitly",
37
+ "Praises the use of a StockObserver interface (programming to an interface, not an implementation)",
38
+ "Praises the removeObserver method — the SKILL.md specifically flags 'Observer memory leaks — registered observers never unregistered' as an anti-pattern to catch",
39
+ "Praises the defensive copy 'new ArrayList<>(tickerObservers)' in notifyObservers, which prevents ConcurrentModificationException if an observer deregisters during notification",
40
+ "Praises per-ticker observer registration — observers only receive events for the tickers they care about",
41
+ "Does NOT manufacture fake issues just to have something to say",
42
+ "May offer optional improvements (e.g., thread safety with CopyOnWriteArrayList, using java.util.EventListener, or noting that prices map isn't thread-safe) but clearly frames these as non-critical suggestions"
43
+ ]
44
+ }
45
+ ]
46
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "pass_rate": 1,
3
+ "passed": 24,
4
+ "total": 24,
5
+ "baseline_pass_rate": 0.667,
6
+ "baseline_passed": 16,
7
+ "baseline_total": 24,
8
+ "delta": 0.333,
9
+ "model": "default",
10
+ "evals_run": 3,
11
+ "date": "2026-03-28",
12
+ "non_standard_provider": true
13
+ }
@@ -0,0 +1,52 @@
1
+ # After
2
+
3
+ Each payment method is extracted into its own `PaymentStrategy` implementation behind a common interface, making it trivial to add new methods without touching existing code.
4
+
5
+ ```kotlin
6
+ // Strategy interface — the contract every payment method must fulfill
7
+ interface PaymentStrategy {
8
+ fun process(order: Order): PaymentResult
9
+ }
10
+
11
+ // One class per payment method — focused, testable, replaceable
12
+ class CreditCardPaymentStrategy : PaymentStrategy {
13
+ override fun process(order: Order): PaymentResult {
14
+ val token = CreditCardGateway.tokenize(order.cardNumber)
15
+ val charge = CreditCardGateway.charge(token, order.totalAmount)
16
+ return PaymentResult(success = charge.success, transactionId = charge.id,
17
+ errorMessage = charge.error.takeIf { !charge.success })
18
+ }
19
+ }
20
+
21
+ class PayPalPaymentStrategy : PaymentStrategy {
22
+ override fun process(order: Order): PaymentResult {
23
+ val session = PayPalClient.createSession(order.paypalEmail)
24
+ val payment = PayPalClient.executePayment(session, order.totalAmount)
25
+ return PaymentResult(success = payment.approved, transactionId = payment.token)
26
+ }
27
+ }
28
+
29
+ class BankTransferPaymentStrategy : PaymentStrategy {
30
+ override fun process(order: Order): PaymentResult {
31
+ val ref = BankTransferService.initiate(order.iban, order.totalAmount)
32
+ return PaymentResult(success = ref != null, transactionId = ref,
33
+ errorMessage = "Bank transfer initiation failed".takeIf { ref == null })
34
+ }
35
+ }
36
+
37
+ // Context: delegates entirely to the injected strategy
38
+ class PaymentProcessor(private val strategy: PaymentStrategy) {
39
+ fun process(order: Order): PaymentResult = strategy.process(order)
40
+ }
41
+
42
+ // Usage — caller selects strategy; PaymentProcessor is unaware of the type
43
+ val processor = PaymentProcessor(CreditCardPaymentStrategy())
44
+ val result = processor.process(order)
45
+ ```
46
+
47
+ Key improvements:
48
+ - If/else chain replaced with Strategy pattern — adding a new payment method requires a new class only, no changes to `PaymentProcessor` (Open-Closed Principle)
49
+ - Each strategy is independently testable with a mock `Order`
50
+ - `PaymentProcessor` depends on the `PaymentStrategy` abstraction, not concrete gateway classes (Dependency Inversion Principle)
51
+ - Responsibility for "how to pay" is encapsulated inside each strategy class (Encapsulate What Varies)
52
+ - Caller selects strategy through constructor injection, enabling runtime switching and easy testing
@@ -0,0 +1,29 @@
1
+ # Before
2
+
3
+ Kotlin payment processing code that uses a brittle if/else chain to select behavior, violating the Open-Closed Principle and making it hard to add new payment methods.
4
+
5
+ ```kotlin
6
+ class PaymentProcessor {
7
+
8
+ fun process(order: Order, method: String): PaymentResult {
9
+ return if (method == "CREDIT_CARD") {
10
+ val token = CreditCardGateway.tokenize(order.cardNumber)
11
+ val charge = CreditCardGateway.charge(token, order.totalAmount)
12
+ if (charge.success) {
13
+ PaymentResult(success = true, transactionId = charge.id)
14
+ } else {
15
+ PaymentResult(success = false, errorMessage = charge.error)
16
+ }
17
+ } else if (method == "PAYPAL") {
18
+ val session = PayPalClient.createSession(order.paypalEmail)
19
+ val payment = PayPalClient.executePayment(session, order.totalAmount)
20
+ PaymentResult(success = payment.approved, transactionId = payment.token)
21
+ } else if (method == "BANK_TRANSFER") {
22
+ val ref = BankTransferService.initiate(order.iban, order.totalAmount)
23
+ PaymentResult(success = ref != null, transactionId = ref)
24
+ } else {
25
+ PaymentResult(success = false, errorMessage = "Unknown payment method: $method")
26
+ }
27
+ }
28
+ }
29
+ ```