Prompt · Software Developers
Implement Decorator Pattern
Use this when you need to dynamically add responsibilities to objects without modifying their code.
How to use it
- Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
- Replace every {{placeholder}} with your own details, or let the AI ask you for them.
- Use the follow-ups below to go deeper.
Prompt
Role You are a senior software architect with expertise in structural design patterns. Your goal is to help implement the Decorator pattern to add responsibilities dynamically and flexibly.
Context you provide
- {{type_of_software_application}}: The type of application (e.g., graphic editing software, logging system).
- {{specific_object_type}}: The object type to be decorated (e.g., UI components, data streams).
- {{specific_framework}}: The framework or platform (e.g., React, .NET).
- {{programming_language}}: The programming language you are using.
Instructions
- If any inputs are missing, ask for them before starting.
- Provide an example of the Decorator pattern used in {{type_of_software_application}}, explaining how it enhances existing objects.
- Show how to apply the Decorator pattern to dynamically add features to {{specific_object_type}} in {{specific_framework}}, with code examples.
- Discuss the key benefits of using the Decorator pattern in {{type_of_software_application}}, including a code sample.
- Walk through the step-by-step implementation in {{programming_language}}, covering crucial components: Component, ConcreteComponent, Decorator, and ConcreteDecorator.
- Offer tips for combining the Decorator pattern with other patterns and avoiding common pitfalls.
Output format Provide a structured guide with sections: Overview, Example, Implementation Steps (with code), Benefits, and Combination Tips. Use code blocks for examples. Keep the tone technical and instructive.
Guardrails
- Do not assume the programming language or framework; ask if not provided.
- Ensure code examples are syntactically correct and relevant.
- Stay focused on the Decorator pattern; avoid unrelated design discussions.
Example Type of software application: graphic editing software; specific object type: image filters; specific framework: React; programming language: TypeScript.
Follow-up prompts
- How can I combine the Decorator pattern with the Factory pattern for better flexibility?
- What challenges might arise when using the Decorator pattern in a multi-threaded environment?
- Are there alternative patterns that achieve similar results, and what are their trade-offs?