{"id":1061,"date":"2026-12-03T08:56:26","date_gmt":"2026-12-03T07:56:26","guid":{"rendered":"https:\/\/metrics.blogg.gu.se\/?p=1061"},"modified":"2026-10-29T10:13:37","modified_gmt":"2026-10-29T09:13:37","slug":"can-we-teach-llms-to-use-design-patterns-a-case-of-singleton","status":"publish","type":"post","link":"https:\/\/metrics.blogg.gu.se\/?p=1061","title":{"rendered":"Can we teach LLMs to use design patterns? A case of Singleton"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/metrics.blogg.gu.se\/files\/2026\/10\/singleton-llm.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/metrics.blogg.gu.se\/files\/2026\/10\/singleton-llm-1024x576.png\" alt=\"\" class=\"wp-image-1062\" srcset=\"https:\/\/metrics.blogg.gu.se\/files\/2026\/10\/singleton-llm-1024x576.png 1024w, https:\/\/metrics.blogg.gu.se\/files\/2026\/10\/singleton-llm-300x169.png 300w, https:\/\/metrics.blogg.gu.se\/files\/2026\/10\/singleton-llm-768x432.png 768w, https:\/\/metrics.blogg.gu.se\/files\/2026\/10\/singleton-llm-1536x864.png 1536w, https:\/\/metrics.blogg.gu.se\/files\/2026\/10\/singleton-llm-1200x675.png 1200w, https:\/\/metrics.blogg.gu.se\/files\/2026\/10\/singleton-llm-1320x743.png 1320w, https:\/\/metrics.blogg.gu.se\/files\/2026\/10\/singleton-llm.png 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>LLMs are good at writing code that works. Give them a task and they will produce a function that passes the tests, most of the time. But software engineering is more than passing tests. It is also about design, about structure and about using the shared language that we, developers, have built over decades. Design patterns are a big part of that language, and LLMs are not very good at following them unless we push them.<\/p>\n\n\n\n<p>So in our latest paper, presented at PROMISE &#8217;26 in Montreal, Viktor Kjellberg, Farnaz Fotrousi and I asked a simple question: how do we guide an LLM so that it actually uses a design pattern? We started with the simplest one we could think of, the Singleton. One class, one instance, one global access point. If the models can&#8217;t do that, they won&#8217;t do Observer or Strategy either.<\/p>\n\n\n\n<p><strong>Link to the paper:<\/strong> https:\/\/doi.org\/10.1145\/3803846.3807469<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What we did<\/h2>\n\n\n\n<p>We took 13 LLMs (GPT-5 mini, GPT-4o mini, GPT-OSS, Llama 3.3, Code Llama, Gemma 3, Qwen3, Qwen3 Coder, Mistral and two DeepSeek models) and asked them to solve the 164 Java tasks in HumanEval-X. First without any guidance, as a baseline. Then with four strategies:<\/p>\n\n\n\n<ol>\n<li><strong>Guiding with instructions<\/strong> \u2013 just tell the model to use Singleton.<\/li>\n\n\n\n<li><strong>Self-improvement<\/strong> \u2013 tell the model, yes or no, whether the code is a Singleton and let it try again (up to 10 times).<\/li>\n\n\n\n<li><strong>Tool-supported self-improvement<\/strong> \u2013 a checker tells the model exactly which property is missing: private constructor, static instance or global access point.<\/li>\n\n\n\n<li><strong>Tool-supported few-shot prompting<\/strong> \u2013 the same feedback, plus two examples of correct Singleton classes.<\/li>\n<\/ol>\n\n\n\n<p>For each strategy we measured two things: how well the code follows Singleton (we call it the Singleton score), and whether the code still passes the tests.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What we found<\/h2>\n\n\n\n<p>Without guidance, no model produced a Singleton. Not a single one. That&#8217;s not surprising, since none of the tasks needs one, but it shows that the models won&#8217;t use a pattern unless asked.<\/p>\n\n\n\n<p>Once asked, things change quickly. Just giving the instruction got Llama 3.3 and Gemma 3 (27B) to a perfect Singleton score. Llama 3.3 also passed 34.1 percentage points more tests than its baseline. GPT-4o mini went from a pass rate of 4.3% to almost 60%. It seems that asking for a bit of structure made some models think harder about the whole solution.<\/p>\n\n\n\n<p>Simple yes\/no feedback worked surprisingly well. Qwen3 (8B) reached a Singleton score of 99.2% and its pass rate went up to 58.6%. Telling the model exactly what was missing pushed even more models to 100%.<\/p>\n\n\n\n<p>The examples, on the other hand, did not help much. For several models the pass rate actually dropped with few-shot prompting. More context is not always better context.<\/p>\n\n\n\n<p>And the results are very model-dependent. DeepSeek Coder and Gemma 3 (4B) got the pattern right but lost functionality. Most of Code Llama&#8217;s compiler errors came from it writing text instead of code. Mistral fulfilled the Singleton properties but 55% of its compiler errors came from referencing libraries that were not there.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What this means<\/h2>\n\n\n\n<p>My takeaway is that we can guide LLMs towards good design with quite simple means. We don&#8217;t need fine-tuning or a big framework. A checker that says &#8220;this is not a Singleton, the constructor should be private&#8221; goes a long way. But there is no single strategy that works for all models, so if you build tools around this, you need to test them with the model you actually use.<\/p>\n\n\n\n<p>Next, we want to look at the more abstract behavioural patterns, like Strategy and Observer. That is where it gets really interesting.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>LLMs are good at writing code that works. Give them a task and they will produce a function that passes the tests, most of the time. But software engineering is more than passing tests. It is also about design, about structure and about using the shared language that we, developers, have built over decades. Design &hellip; <a href=\"https:\/\/metrics.blogg.gu.se\/?p=1061\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Can we teach LLMs to use design patterns? A case of Singleton&#8221;<\/span><\/a><\/p>\n","protected":false},"author":68,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/metrics.blogg.gu.se\/index.php?rest_route=\/wp\/v2\/posts\/1061"}],"collection":[{"href":"https:\/\/metrics.blogg.gu.se\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metrics.blogg.gu.se\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metrics.blogg.gu.se\/index.php?rest_route=\/wp\/v2\/users\/68"}],"replies":[{"embeddable":true,"href":"https:\/\/metrics.blogg.gu.se\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1061"}],"version-history":[{"count":1,"href":"https:\/\/metrics.blogg.gu.se\/index.php?rest_route=\/wp\/v2\/posts\/1061\/revisions"}],"predecessor-version":[{"id":1063,"href":"https:\/\/metrics.blogg.gu.se\/index.php?rest_route=\/wp\/v2\/posts\/1061\/revisions\/1063"}],"wp:attachment":[{"href":"https:\/\/metrics.blogg.gu.se\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metrics.blogg.gu.se\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metrics.blogg.gu.se\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}