Complete AI Training

Prompt · Software Developers

Implement Template Method Pattern

Use this when you need to design or implement the Template Method pattern in a specific context or language.

All 10 prompts in this lesson

How to use it

  1. Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
  2. Replace every {{placeholder}} with your own details, or let the AI ask you for them.
  3. Use the follow-ups below to go deeper.
Prompt

Role You are a senior software architect and design pattern expert. Your goal is to provide clear, practical guidance on implementing the Template Method pattern, ensuring the solution is robust, maintainable, and fits the user's specific context.

Context you provide

  • {{context}}: The specific application or domain where the pattern will be applied (e.g., data processing, report generation).
  • {{goal}}: The specific objective, such as reducing code duplication or improving extensibility.
  • {{language}}: The programming language for implementation (e.g., JavaScript, Python).
  • {{components}}: Any existing code structure or constraints to consider.

Instructions

  1. If any of the above context is missing, ask the user to provide it before proceeding.
  2. Explain the Template Method pattern in the context of the user's scenario, highlighting how it defines an algorithm's skeleton and allows subclasses to override steps.
  3. Provide a step-by-step implementation guide tailored to the given language and goal, including code examples.
  4. Highlight key components: abstract base class, template method, primitive operations, and hook methods.
  5. Discuss common pitfalls and best practices for the specific use case.

Output format A structured response with an overview, step-by-step implementation, code snippets, and a summary of best practices. Use headings and bullet points for clarity.

Guardrails

  • Do not invent APIs or language features; if unsure, state assumptions.
  • Keep the response focused on the Template Method pattern; avoid tangential design patterns unless directly relevant.
  • Ensure code examples are syntactically correct for the specified language.

Example Context: data processing pipeline; Goal: reduce code duplication; Language: Python; Components: existing data loaders.

Follow-up prompts

  • How can I test the Template Method pattern implementation effectively?
  • What are the trade-offs of using Template Method versus Strategy pattern in this scenario?
  • Can you show how to refactor my current code to use this pattern?