arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2605.26898cs.SEcs.AI
收藏

引导LLM使用软件设计模式的策略:以单例模式为例

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

University of Gothenburg and Chalmers University of Technology(哥德堡大学和查尔姆斯理工大学)

Viktor Kjellberg, Farnaz Fotrousi, Miroslaw Staron

AI总结:

通过实验比较四种提示策略(指令、二元自动反馈、详细自动反馈、少样本详细反馈),评估13个LLM在164个Java编码挑战中生成遵循单例模式的代码的能力,发现迭代二元反馈在保持或提升功能性的同时最佳地实现了单例模式对齐。

摘要:

大型语言模型(LLM)可以从自然语言提示生成功能性源代码,但往往无法一致地遵循更高级别的架构结构或设计模式。由于LLM在软件工程中的应用日益增多,它们将既定设计原则应用于生成代码的能力对于软件产品的长期成功至关重要。因此,本文的目标是确定引导LLM将设计模式融入生成源代码的策略。我们设计了一个计算实验,评估13个LLM生成遵循单例设计模式的代码的能力,使用了四种提示策略:指令、二元自动反馈、详细自动反馈以及带少样本提示的详细反馈,在HumanEval-X的164个Java编码挑战中进行。我们的结果表明,引导LLM包含设计模式的最佳策略在很大程度上取决于模型类型。尽管如此,总体而言,迭代二元反馈在保持或改善代码功能性的同时,提供了与单例模式的最佳对齐。通过指令引导,Llama 3.3在100%的情况下生成了单例类,并改善了代码功能性,使通过的测试数量增加了34.1个百分点。通过指令和二元反馈引导,它取得了类似的结果。Qwen 3(8B)使用二元反馈将单例模式对齐度提高到99.2%,功能性提高到58.6%。我们的结果表明,即使是简单的策略也可以用于引导LLM使用设计模式。

英文摘要:

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.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑