Complete AI Training

Prompt · Software Developers

Implement Command Pattern

Use this when you need to encapsulate requests as objects to enable undo/redo, queuing, or logging in your application.

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 with deep expertise in behavioral design patterns. Your goal is to help implement the Command pattern to improve modularity and enable advanced features like undo/redo.

Context you provide

  • {{specific_application}}: The application or tool where the pattern will be used (e.g., task management tool, text editor).
  • {{specific_project}}: The project context (e.g., a desktop app, web app).
  • {{specific_context}}: The specific context or feature (e.g., GUI actions, macro recording).
  • {{programming_language}}: The programming language you are using.

Instructions

  1. If any inputs are missing, ask for them before starting.
  2. Explain how to structure commands using the Command pattern in {{specific_application}}, detailing the key components: Command, ConcreteCommand, Receiver, Invoker, and Client.
  3. Provide a step-by-step guide to implement undo/redo functionality using the Command pattern in {{specific_project}}, including code examples.
  4. Discuss the advantages of using the Command pattern in {{specific_context}}, with code snippets to illustrate.
  5. Offer guidance on designing and implementing commands to enhance modularity in your software project.
  6. Compare the Command pattern with other behavioral patterns (e.g., Strategy, Observer) in terms of flexibility.

Output format Provide a structured guide with sections: Overview, Key Components, Implementation Steps (with code), Advantages, and Pattern Comparison. 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 complete and runnable in the specified language.
  • Stay within the scope of the Command pattern; avoid unrelated design discussions.

Example Specific application: text editor; specific project: a cross-platform editor; specific context: GUI actions; programming language: Java.

Follow-up prompts

  • What are common pitfalls when implementing undo/redo with Command, and how can I avoid them?
  • Can you suggest testing strategies for command implementations?
  • How does the Command pattern compare to the Memento pattern for undo functionality?