Meaning
A refactoring technique that moves responsibilities from an overloaded class into a new, focused class to improve cohesion and reduce coupling.
Primary Function
Improve modularity by extracting a cohesive set of fields and methods from a bloated class into a new collaborator class.
Communicative Purpose
Communicate the intent to refactor a large class into smaller, more cohesive components to improve maintainability.
Pattern
Extract Class
Core Structure
Create a new class, move related fields and methods from the source class to the new class, update references, and establish a relationship (e.g., composition or delegation) between the two classes.
Função primária
Improve modularity by extracting a cohesive set of fields and methods from a bloated class into a new collaborator class.
Propósito comunicativo
Communicate the intent to refactor a large class into smaller, more cohesive components to improve maintainability.
Situações de gatilho
A class has grown too large, exhibits low cohesion, or groups of fields and methods always appear together (data clumps).
Contextos
Object-oriented codebases undergoing refactoring; legacy code maintenance; preparing for reuse or testing.
Padrão
Extract Class
Estrutura central
Create a new class, move related fields and methods from the source class to the new class, update references, and establish a relationship (e.g., composition or delegation) between the two classes.
Slots de substituição
{"SourceClass":"The original class with too many responsibilities","NewClass":"The new class that will hold the extracted responsibilities","fieldOrMethod":"A field or method moved from SourceClass to NewClass"}
Colocados típicos
- refactoring refactor class method field encapsulation cohesion coupling composition delegation
Substituições comuns
- Extract Class → Replace Data Value with Object Extract Class → Introduce Parameter Object Extract Class → Replace Type Code with Subclasses
Erros comuns
Moving too little or too much behavior, resulting in weak cohesion Failing to update all references to moved members Creating a new class that still has low cohesion Introducing unnecessary indirection
Similar / contraste
{"similar":"Inline Class","contrast":"Inline Class does the opposite: moves responsibilities from a class back into its caller."} {"similar":"Extract Interface","contrast":"Extract Interface focuses on extracting a contract (interface) rather than implementation."}
Interferências
Do not confuse with Extract Method, which operates within a single class. Avoid extracting classes that are too tightly coupled to the original class, leading to bidirectional dependencies.
Família do chunk
- Refactoring
- Object-Oriented Design
- Class Responsibility
Nuance
The goal is to improve design quality, not just to split code arbitrarily; the extracted class should have a clear, single responsibility.
Efeito pragmático
Improves readability, testability, and maintainability by reducing class size and increasing cohesion.
Dica de memória
Think of 'extracting' a cohesive chunk of responsibilities out of a bloated class, like pulling out a ingredient to make a new dish.
Nota
Extract Class is one of the classic refactorings described in Martin Fowler's 'Refactoring: Improving the Design of Existing Code'.
Upgrade path
Consider learning 'Inline Class' (the opposite refactoring) and 'Extract Interface' for further design improvements.
Log in to save chunks.