MCPcopy Index your code
hub / github.com/RefactoringGuru/design-patterns-python

github.com/RefactoringGuru/design-patterns-python @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
307 symbols 640 edges 24 files 127 documented · 41%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Design Patterns in Python

This repository is part of the Refactoring.Guru project.

It contains Python examples for all classic GoF design patterns.

Each pattern includes two examples:

  • [x] Conceptual examples show the internal structure of patterns, including detailed comments.

  • [ ] RealWorld examples show how patterns can be used in real-world Python applications.

Requirements

These examples require Python 3.7 and newer.

All examples can be launched via the command line, using the Python executable as follows:

python src/Path-to-example/main.py

For the best experience, I recommend working with examples with these IDEs:

FAQ

1. What is the Client Code?

Client means client of classes, defined as part of a pattern, which is merely a caller of the given methods or a user of the given classes. In other words, it's the part of your application's code that uses the pattern's classes.

2. I don't understand the roles you're referring to in RealWorld examples.

Take a look at the conceptual example first. There you'll find detailed descriptions of each class in a pattern, its role, and connection to other classes.

Contributor's Guide

I appreciate any help, whether it's a simple fix of a typo or a whole new example. Just make a fork, make your change and submit a pull request.

Here's a style guide which might help you to keep your changes consistent with the rest of the project's code:

  1. All code should match the PEP 8 coding style guide

  2. Try to hard-wrap the code at 80th's character. It helps to list the code on the website without scrollbars.

  3. Aim to put all code within one file. Yes, I realize that it's not how it supposed to be done in production. However, it helps people to understand examples better, since all code fits into one screen.

  4. Comments may or may not have language tags in them, such as this:

    ```python """ EN: All products families have the same varieties (MacOS/Windows).

    This is a MacOS variant of a button.

    RU: Все семейства продуктов имеют одни и те же вариации (MacOS/Windows).

    Это вариант кнопки под MacOS. """ ```

    This notation helps to keep the code in one place while allowing the website to generates separate versions of examples for all listed languages. Don't be scared and ignore the non-English part of such comments. If you want to change something in a comment like this, just do it. Even if you do it wrong, we'll tell you how to fix it during the Pull Request.

License

This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

Creative Commons License

Credits

Authors: Alexey Pyltsyn (@lex111) and Alexander Shvets (@neochief)

Core symbols most depended-on inside this repo

add
called by 6
src/Composite/Conceptual/main.py
handle
called by 5
src/ChainOfResponsibility/Conceptual/main.py
add
called by 5
src/Builder/Conceptual/main.py
operation
called by 4
src/Decorator/Conceptual/main.py
notify
called by 4
src/Mediator/Conceptual/main.py
add_item
called by 3
src/Iterator/Conceptual/main.py
some_business_logic
called by 3
src/Observer/Conceptual/main.py
transition_to
called by 3
src/State/Conceptual/main.py

Shape

Method 197
Class 95
Function 15

Languages

Python100%

Modules by API surface

src/AbstractFactory/Conceptual/main.py25 symbols
src/Builder/Conceptual/main.py21 symbols
src/Memento/Conceptual/main.py19 symbols
src/Visitor/Conceptual/main.py18 symbols
src/TemplateMethod/Conceptual/main.py17 symbols
src/Composite/Conceptual/main.py16 symbols
src/State/Conceptual/main.py15 symbols
src/Observer/Conceptual/main.py15 symbols
src/Command/Conceptual/main.py15 symbols
src/Mediator/Conceptual/main.py14 symbols
src/FactoryMethod/Conceptual/main.py14 symbols
src/Decorator/Conceptual/main.py13 symbols

For agents

$ claude mcp add design-patterns-python \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact