Meaning
Move Method is a refactoring technique that relocates a method from the class where it is currently defined to another class where it has a stronger relationship, typically because the method uses or is used more by features of the target class.
Primary Function
Improve cohesion and reduce coupling by placing a method in the class that is most closely related to its behavior.
Communicative Purpose
Communicates the intent to reassign responsibility between classes to achieve better encapsulation and clearer design.
Pattern
Move a method from its current class to another class where it has a stronger relationship, updating all call sites to invoke the method on the target class (or via delegation).
Core Structure
The source class loses the method definition; the target class gains the method; callers are updated to call the method on the target class (sometimes via a delegating wrapper).
Função primária
Improve cohesion and reduce coupling by placing a method in the class that is most closely related to its behavior.
Propósito comunicativo
Communicates the intent to reassign responsibility between classes to achieve better encapsulation and clearer design.
Situações de gatilho
When a method accesses or is accessed more by another class than its own class, or when the method does not use any private data of its current class.
Contextos
Object-oriented programming, refactoring sessions, code reviews, legacy code improvement.
Padrão
Move a method from its current class to another class where it has a stronger relationship, updating all call sites to invoke the method on the target class (or via delegation).
Estrutura central
The source class loses the method definition; the target class gains the method; callers are updated to call the method on the target class (sometimes via a delegating wrapper).
Colocados típicos
- Extract Method
- Inline Method
- Move Field
- Pull Up Method
- Push Down Method
Substituições comuns
- Move Method with delegation – leaves a delegating wrapper
- Move Method with parameterization – adds parameters for needed data
- Rename Method – if only naming is the issue
Erros comuns
Moving a method that still depends on private fields of the source class without passing those fields as parameters, leading to broken encapsulation; moving an overridden method without adjusting subclass overrides.
Similar / contraste
Extract Method (decomposition), Inline Method (inverse), Move Field (similar concept for fields).
Interferências
Moving a method that is overridden in subclasses can break polymorphism; ensure subclasses are updated or consider using the Template Method pattern.
Família do chunk
- Extract Method
- Inline Method
- Move Field
- Extract Class
Nuance
If the method accesses instance fields of the source class, consider moving those fields as well or passing them as parameters to preserve behavior.
Efeito pragmático
Increases class cohesion, reduces inter-class coupling, and makes the responsibility distribution clearer and more maintainable.
Dica de memória
Think of moving a tool to the workshop where it is used most often.
Nota
After moving a method, update all related unit tests and documentation to reflect its new location.
Upgrade path
After moving a method, consider applying Extract Method to break down large methods, or Inline Method if the moved method becomes trivial.
Log in to save chunks.