Refactor Monolithic God Classes into Clean Services

Get a step-by-step plan to break a large monolithic class into smaller, testable services while preserving the original public API behavior.

Agent prompt for ChatGPT and Claude

Prompt

Prompt
Act as a Senior Software Architect. Your task is to refactor a monolithic class into smaller, single-responsibility services using dependency injection. First, ask me these three questions: 1. Paste the code of the monolithic class you want to refactor. 2. List the primary responsibilities currently handled by this class. 3. Name the programming language and framework you are using. After I answer, provide a refactoring plan. Rules: 1. Identify logical groupings for new services. 2. Define an interface for each new service. 3. Update the original class to act as a facade that delegates calls to these new services to ensure no breaking changes to the existing public API. 4. Show the new dependency injection structure. Output format: # Refactoring Strategy, ## Proposed Services, ## Interface Definitions, ## Refactored Facade Implementation. This is for educational purposes and requires code review by a human lead developer before deployment.

Example output

Example for: a 1200-line OrderManager class in a C# e-commerce backend.

Refactoring Strategy

Extract the monolithic OrderManager into three distinct services: OrderValidator, PaymentProcessor, and NotificationService. The original OrderManager will remain as a facade to maintain backward compatibility for existing controllers.

Proposed Services

  • OrderValidator: Handles business rules and SKU availability checks.
  • PaymentProcessor: Manages gateway communication and transaction logging.
  • NotificationService: Handles email and SMS dispatch logic.

Interface Definitions

public interface IOrderValidator { bool Validate(Order order); }
public interface IPaymentProcessor { bool Process(Order order); }
public interface INotificationService { void SendConfirmation(Order order); }

Refactored Facade Implementation

public class OrderManager : IOrderManager {
private readonly IOrderValidator _validator;
private readonly IPaymentProcessor _payment;
private readonly INotificationService _notifier;

public OrderManager(IOrderValidator v, IPaymentProcessor p, INotificationService n) {
_validator = v; _payment = p; _notifier = n;
}

public bool ProcessOrder(Order order) {
if (!_validator.Validate(order)) return false;
if (!_payment.Process(order)) return false;
_notifier.SendConfirmation(order);
return true;
}
}

Written by AI from this prompt. Your results will vary.

Tips and common edits

Why this works

  • Forces a clear separation of concerns by identifying logical groupings before writing code.
  • Ensures backward compatibility by keeping the original class as a facade.
  • Uses dependency injection to improve testability and modularity.

Use this with GitHub Copilot or ChatGPT when your code has become too large to maintain. Paste your monolithic class when prompted. It works best for languages like Java, C#, or TypeScript where interfaces are standard.

Common edits

  • Add specific design patterns like Strategy or Factory if the logic requires complex conditional branches.
  • Adjust the interface definitions to include async/await patterns if the project requires high concurrency.

Reuse it: Use in a workflowAdapt in the builder

Prompt details

By
Prompts-GPT editors
Published
How prompts are written and checked

Did this prompt work?

Coding

Plan an incremental refactor for legacy code

Break down monolith or fragile legacy modules into safe, testable milestones using branch-by-abstraction patterns.

Compare Three Feature Architectures Against Module Boundaries

Get a scored trade-off matrix comparing three feature architectures by file touchpoints, coupling risks, and rollback cost before running Cursor Composer.

Automated Dead Code Cleanup Plan for Codex Repositories

Get a systematic plan to identify and safely remove unused exports and dead code from your codebase using characterization tests to prevent regressions.

Coding

Refactor Pasted Code for Readability Without Changing Its Behavior

Returns a cleaner version of your code plus a change log that explains each edit, why behavior is preserved, and which edge cases you should test.

Coding

Refactor nested legacy loops into flat readable helper functions

Decompose deeply nested legacy loops into single-purpose functions. Use this when reviewing complex procedural code that is hard to test or debug.