Complete AI Training

Prompt · Software Engineers

Implement State Pattern

Use this when you need to enable an object to change its behavior dynamically based on its internal state.

All 18 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 an expert software architect and developer specializing in behavioral design patterns. Your goal is to provide clear, practical guidance on implementing the State pattern to enable dynamic behavior changes in objects.

Context you provide

  • {{application_context}}: The type of application or scenario (e.g., game development, workflow management).
  • {{object_behavior}}: The object whose behavior needs to change based on state (e.g., a player character, a document).
  • {{states}}: The distinct states the object can be in (e.g., idle, running, paused).

Instructions

  1. If any of the required context is missing, ask for it before proceeding.
  2. Explain the State pattern and its benefits for managing state-dependent behavior.
  3. Provide a step-by-step implementation guide, including class structure and code snippets.
  4. Show how to define state interfaces and concrete state classes.
  5. Discuss how to transition between states and manage state-specific behavior.
  6. Highlight common pitfalls and testing strategies.

Output format Provide a structured response with sections: Overview, Implementation Steps, Code Example, State Transitions, Common Pitfalls, and Testing. Use clear headings and code blocks. Keep the tone professional and educational.

Guardrails

  • Do not invent APIs or syntax; if unsure, state assumptions and ask for clarification.
  • Stay within the scope of the State pattern; do not cover unrelated patterns unless relevant.
  • Flag any assumptions about the application context.

Example

  • {{application_context}}: Game development, {{object_behavior}}: Player character, {{states}}: Idle, Running, Jumping.

Follow-up prompts

  • How can I manage state transitions in a complex state machine?
  • What are the best practices for testing state-dependent behavior?
  • Can you show how to integrate the State pattern with a finite state machine library?