Do Code LLMs Understand Design Patterns?

Zhenyu Pan, Xuefeng Song, Yunkun Wang, Rongyu Cao, Binhua Li, Yongbin Li, Han Liu

TL;DR

While Code Large Language Models (LLMs) are becoming staples in the developer's toolkit, they still struggle significantly with the "blueprints" of software engineering: design patterns. A new empirical study reveals that even top-tier models like GPT-4o and Llama-3.1-70B achieve less than 40% accuracy in identifying design patterns, often defaulting to common patterns like "Singleton" or "Factory" even when they don't fit the context.

What Problem Does This Paper Address?

In professional software development, code isn't just about logic; it’s about structure. Design patterns (like Singleton, Factory, or Observer) provide standardized solutions to common architectural problems, ensuring code is maintainable and scalable.

The authors identify a growing "mismatch" problem: while LLMs are great at generating snippets of functional code, they often fail to respect the existing architectural norms of a project. If a project uses a specific Factory pattern for object creation and an LLM generates a direct instantiation, the developer is left with the "additional burden" of refactoring that code to fit the project's standards. This paper seeks to quantify exactly how well (or poorly) current models understand these structural foundations.

Methods

The researchers evaluated 11 state-of-the-art models (including the GPT-4 series, Claude 3.5, Llama 3.1, and CodeQwen) across three distinct tasks:

  1. Design Pattern Classification: Can the model identify which of 12 patterns is being used in a provided code file?
  2. Line Completion: Can the model fill in missing lines of code while maintaining the structural integrity of a specific pattern?
  3. Function Generation: Can the model generate an entire function from a description? This was tested in two modes: one where the model was told which design pattern to use, and one where it had to infer it from context.

To measure success, they used standard Accuracy for classification, and for generation, they used Code Similarity (CS) and Edit Similarity (ES)—the latter measuring how much manual effort a human would need to "fix" the generated code to match the reference.

Datasets

The authors curated a new dataset by manually selecting 48 high-quality repositories from GitHub.

  • Languages: Java and Python (24 repos each).
  • Patterns: 12 classic design patterns (e.g., Singleton, Factory, Observer, Facade, Decorator, etc.).
  • Complexity: Files were categorized into three levels: Simple, Moderate, and Difficult.
  • Uniqueness: Unlike many benchmarks that use synthetic code, this study used real-world, "high-quality" open-source code to ensure practical relevance.

Key Insights and Ideas

  • The "Hammer and Nail" Bias: Models exhibit a strong bias toward the most common patterns. They frequently over-predict "Singleton" and "Factory" because these appear most often in training data.
  • The Java Advantage: Models performed significantly better on Java code than Python. The authors suggest this is because Java’s syntax is more verbose and explicitly structured around object-oriented patterns, whereas Python’s flexibility makes patterns more "subtle" and harder for models to spot.
  • The "Facade" Failure: Patterns like "Facade" are particularly difficult for LLMs. Because Facade is an abstract concept (providing a unified interface to a set of interfaces) without a single "smoking gun" structural marker, models often fail to recognize it without deep contextual understanding.
  • Similarity $\neq$ Utility: A high "Code Similarity" score didn't always mean the code was easy to use. Sometimes a model would generate code that looked like the original but required significant manual edits (low Edit Similarity) to actually function within the project's architecture.

Key Results

  • Low Ceiling for Accuracy: The best-performing models, GPT-4o and Llama-3.1-70B, only achieved an overall classification accuracy of 38.81%.
  • Complexity Gap: Performance plummeted as code complexity increased. On "Java Easy" datasets, top models hit ~71% accuracy, but on "Python Hard" datasets, many models (including GPT-4) scored 0%.
  • Internalized Patterns: Interestingly, some models (like GPT-4o and Llama-3.1-405B) actually performed better at code completion when they weren't explicitly told which pattern to use, suggesting they rely more on internalized training patterns than on following specific instructions in a prompt.

Limitations and Future Work

The study is currently limited to 12 design patterns and two programming languages. The authors acknowledge that real-world software often combines multiple patterns in a single file, a scenario this study didn't fully explore.

Future work aims to expand the dataset to include more object-oriented languages (like C++ or C#) and to investigate how to better "guide" models to follow development standards during the training or fine-tuning phase, rather than just relying on prompt engineering.

Summary updated

BibTeX
@inproceedings{pan2025do,
  author = {Zhenyu Pan and Xuefeng Song and Yunkun Wang and Rongyu Cao and Binhua Li and Yongbin Li and Han Liu},
  title = {{Do Code LLMs Understand Design Patterns?}},
  booktitle = {LLM4Code@ICSE},
  year = {2025},
  doi = {10.1109/llm4code66737.2025.00031},
}

Loading related content...

Figure