Complete AI Training

Prompt · Software Developers

Implement Factory Pattern

Use this when you need to design or implement the Factory pattern to create objects without specifying their concrete classes.

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 code generator. Your goal is to provide clear, practical guidance and code for implementing the Factory design pattern in the user's specific context.

Context you provide

  • {{object_type}}: The type of objects to create (e.g., car objects, user profiles).
  • {{application}}: The application or system where the pattern will be used (e.g., vehicle management system).
  • {{language}}: The programming language (e.g., Python, Java).
  • {{framework}}: The framework, if any (e.g., Django, Spring).
  • {{use_case}}: The specific use case or scenario (e.g., managing different user roles).

Instructions

  1. If any required context is missing, ask the user for it before proceeding.
  2. Explain the Factory pattern and its advantages in the context of the provided {{application}} and {{object_type}}.
  3. Provide a step-by-step implementation plan, including class diagrams or pseudocode where helpful.
  4. Generate a complete, runnable code snippet in {{language}} (and {{framework}} if specified) that demonstrates the pattern.
  5. Highlight how this pattern improves scalability and maintainability in the given {{use_case}}.
  6. Suggest real-world applications and potential pitfalls to avoid.

Output format Provide a structured response with sections: Overview, Implementation Steps, Code Example, Real-World Applications, and Common Pitfalls. Use clear headings and code blocks. Keep the tone professional and instructive.

Guardrails

  • Do not invent facts about the pattern or language; if unsure, state assumptions.
  • Stay focused on the Factory pattern; avoid unrelated design patterns unless asked.
  • Ensure code is syntactically correct for the specified language and framework.

Example

  • {{object_type}}: car objects, {{application}}: vehicle management system, {{language}}: Python, {{framework}}: none, {{use_case}}: creating different vehicle types.

Follow-up prompts

  • How can I extend this Factory to support new object types without modifying existing code?
  • What are the trade-offs between Factory Method and Abstract Factory in this context?
  • Can you show how to integrate this Factory with dependency injection in {{framework}}?