← Latest papers
🤖 AI

Strategies for Guiding LLMs to Use Software Design Patterns: A Case of Singleton

This paper evaluates four prompting strategies across 13 large language models on 164 Java challenges to determine how best to guide them in implementing the Singleton design pattern, finding that iterative binary feedback is generally optimal while specific instruction-based approaches can achieve near-perfect pattern adherence and significantly improve code functionality depending on the model.

Original authors: Viktor Kjellberg, Farnaz Fotrousi, Miroslaw Staron

Published 2026-05-27
📖 5 min read🧠 Deep dive

Original authors: Viktor Kjellberg, Farnaz Fotrousi, Miroslaw Staron

Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer

Imagine you have a team of incredibly talented but slightly chaotic junior chefs (these are the Large Language Models, or LLMs). They are amazing at following recipes to cook a delicious meal (writing functional code). However, they often forget the "rules of the kitchen" regarding how the kitchen itself should be organized. They might cook a great steak, but they might leave the stove on, use three different pots for one dish, or forget to lock the back door.

In the world of software, these "rules of the kitchen" are called Design Patterns. One specific rule is called the Singleton Pattern. Think of the Singleton Pattern as a rule that says: "There can only be one master key for the building, and everyone must get their copy from that single key." It ensures that a specific part of the software (like a car engine or a database connection) is only created once and shared by everyone, rather than making ten different engines for one car.

The problem the researchers found is that while these AI chefs can cook the meal, they often forget to follow the "One Master Key" rule unless you explicitly tell them to.

The Experiment: Teaching the Chefs the Rules

The researchers wanted to find the best way to teach these AI chefs to follow the "One Master Key" rule without ruining the meal they were cooking. They tested 13 different AI models (from different "families" like GPT, Llama, and Qwen) using 164 different cooking challenges (coding tasks).

They tried four different teaching strategies:

  1. The "Just Do It" Instruction: They simply told the chef, "Hey, make sure you use the One Master Key rule."

    • The Result: Some chefs understood immediately and did a perfect job. Others got confused and started making mistakes in the actual cooking (the code stopped working). It depended entirely on which chef you asked.
  2. The "Yes/No" Feedback Loop: The chef cooked, and a robot inspector checked the kitchen. If the chef didn't use the One Master Key rule, the robot just said, "No, try again." The chef had to guess what was wrong and try again.

    • The Result: This helped a lot. The chefs learned from their mistakes and eventually got the rule right. For some, it actually made the meal taste better (the code worked even more reliably).
  3. The "Detailed Report" Feedback Loop: This was like the robot inspector giving a specific checklist. Instead of just saying "No," it said, "You forgot the private lock on the door," or "You didn't put the key in the main hall."

    • The Result: This was the most helpful for most chefs. By knowing exactly what was missing, they could fix it precisely. This strategy got the highest number of chefs to follow the rule perfectly.
  4. The "Show and Tell" (Few-Shot) Method: The researchers showed the chef two examples of perfect kitchens that used the One Master Key rule, then asked them to try again.

    • The Result: This was a mixed bag. Some chefs loved the examples and did great. Others got confused by the examples and actually performed worse than before.

The Big Takeaways

The paper concludes with three main points, translated into our kitchen analogy:

  • You can teach them, but the method matters: You can definitely get these AI chefs to follow the "One Master Key" rule. However, the best way to teach them depends on which chef you are using. A strategy that works perfectly for Chef A might confuse Chef B.
  • Feedback is the secret sauce: Simply telling them to do it is okay, but giving them feedback on what they did wrong is much better. If you tell them exactly which part of the rule they missed (like "you forgot the lock"), they learn faster and make fewer mistakes.
  • Don't break the meal: The researchers were worried that forcing the chefs to follow this new rule might ruin the taste of the meal (break the code's functionality). Surprisingly, for many of the top chefs, following the rule actually made the meal better. However, for some weaker chefs, trying to follow the rule made the meal inedible.

The Bottom Line

The researchers found that you don't need complex magic to get AI to follow software design rules. Sometimes, just giving it a clear instruction works. Other times, you need to act like a strict but helpful teacher, pointing out exactly what was missing. The key is that there is no "one size fits all" solution; you have to pick the teaching style that works best for the specific AI model you are using.

Note: The paper only tested this on Java code (a specific programming language) and only on the "Singleton" rule. They did not test this on medical diagnoses, financial advice, or other real-world applications. They simply proved that with the right prompting, AI can learn to follow specific architectural rules in code.

Drowning in papers in your field?

Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.

Try Digest →