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

📅 2026-05-26
📈 Citations: 0
Influential: 0
📄 PDF
🤖 AI Summary
This study addresses the challenge that large language models (LLMs) often fail to consistently adhere to software design patterns during code generation, thereby compromising architectural quality. To mitigate this issue, the authors propose four prompting strategies—instructional prompting, binary automated feedback, detailed automated feedback, and feedback augmented with few-shot examples—and evaluate their effectiveness across 13 LLMs on 164 Java tasks from the HumanEval-X benchmark, focusing specifically on the Singleton pattern. Experimental results reveal substantial variation in how different models respond to these strategies, demonstrating that lightweight feedback mechanisms can effectively guide adherence to design patterns. Notably, Llama 3.3 achieves 100% structural compliance with the Singleton pattern under instructional prompting, yielding a 34.1 percentage point increase in test pass rate, while Qwen 3 (8B) attains 99.2% pattern alignment and 58.6% functional correctness using binary feedback.
📝 Abstract
Large Language Models (LLMs) can generate functional source code from natural-language prompts, but often fail to consistently follow higher-level architectural structures or design patterns. Since LLMs are increasingly used in software engineering, their ability to apply established design principles to generated code is crucial to the long-term success of software products. Therefore, the goal of this paper is to identify strategies for guiding LLMs to incorporate design patterns into the generated source code. We designed a computational experiment to evaluate the ability of 13 LLMs to generate code that follows the Singleton design pattern, using four prompting strategies: instructions, binary automated feedback, extensive automated feedback, and extensive feedback with few-shot prompts, in 164 Java coding challenges from HumanEval-X. Our results shows that the optimal strategy to guide LLMs to include design patterns depends heavily on the type of model. Still, overall, iterative binary feedback provides the best alignment with Singleton while preserving or improving the code's functionality. With guiding with instructions, Llama 3.3 generated Singleton classes in 100% of cases and improved code functionality, increasing the number of tests passed by 34.1 percentage points. It achieved a similar result with guidance through instructions and binary feedback. Qwen 3 (8B) increased the alignment with Singleton to 99.2% and the functionality to 58.6% using binary feedback. Our result suggests that even simple strategies can be used to guide LLMs to use design patterns.
Problem

Research questions and friction points this paper is trying to address.

Large Language Models
Software Design Patterns
Singleton Pattern
Code Generation
Prompting Strategies
Innovation

Methods, ideas, or system contributions that make the work stand out.

design patterns
large language models
prompting strategies
Singleton pattern
code generation
🔎 Similar Papers
No similar papers found.