Can we teach LLMs to use design patterns? A case of Singleton

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.

So in our latest paper, presented at PROMISE ’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’t do that, they won’t do Observer or Strategy either.

Link to the paper: https://doi.org/10.1145/3803846.3807469

What we did

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:

  1. Guiding with instructions – just tell the model to use Singleton.
  2. Self-improvement – tell the model, yes or no, whether the code is a Singleton and let it try again (up to 10 times).
  3. Tool-supported self-improvement – a checker tells the model exactly which property is missing: private constructor, static instance or global access point.
  4. Tool-supported few-shot prompting – the same feedback, plus two examples of correct Singleton classes.

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.

What we found

Without guidance, no model produced a Singleton. Not a single one. That’s not surprising, since none of the tasks needs one, but it shows that the models won’t use a pattern unless asked.

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.

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%.

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.

And the results are very model-dependent. DeepSeek Coder and Gemma 3 (4B) got the pattern right but lost functionality. Most of Code Llama’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.

What this means

My takeaway is that we can guide LLMs towards good design with quite simple means. We don’t need fine-tuning or a big framework. A checker that says “this is not a Singleton, the constructor should be private” 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.

Next, we want to look at the more abstract behavioural patterns, like Strategy and Observer. That is where it gets really interesting.

Author: Miroslaw Staron

I’m professor in Software Engineering at Computer Science and Engineering. I usually blog about interesting articles (for me) and my own reflections on the development of Software Engineering, AI, computer science and automotive software.