Encapsulate Variable / Encapsulate Field
Identify a variable or field that has public or global access that you would like to encapsulate.
Find all the places it is used and store this list or the means to re-create it.
- IDE: with the cursor on the function declaration, Find Usages.
- Alternatively - “lean on the compiler” - rename it and see what breaks.
- Create a getter and setter function for the field you want to encapsulate.
- IDE: with the cursor on the field, “Refactor, Encapsulate” or “Generate… Getter and Setter”
- Compile / run static checks.
- Go through each reference to the variable or field and replace it with a call to the getter or setter function. Use the list you made earlier.
- Test after each replacement.
At this point you’ve succeeded in you original aim - the global variable is encapsulated by getter and setter functions.
Change the encapsulated field or variable to be private (if your language allows).
- If you have several related fields, you could Extract Class. Give it a good name.
- Now it is encapsulated, you can more easily change the type of the field. For example Replace Primitive with Object.
Prompted by Code Smells
Relevant Learning Hours
- This refactoring is included Martin Fowler's book "Refactoring", 2nd Edition
Back to All Refactorings