Abstraction refactorings increase the ability for code to represent its meaning while abstracting away implementation details.
Classes should not give direct access to their fields. Encapsulate Field refactors fields to property methods, allowing for an abstraction of the implementation details.
Listing 1 – Class giving direct access to fields
After applying the Encapsulate Field Refactoring, there is now a level of isolation between the class’ fields and calling classes.
Listing 2 – Class with Encapsulated Fields
JustCode Steps: “Convert field to auto-implemented property” and “Convert to property with backing field” refactorings
Extract Interface Refactoring
Extracting a class’ interface helps to follow the Dependency Inversion Principle by abstracting the implementation details away from using the class.
Listing 3 – Interface for class in Listing 2
JustCode Steps: “Extract Interface…”
Convert Interface to Abstract Class
Interfaces do not contain implementation details, and are the ultimate abstraction. Sometimes, some level of shared implementation is required, and abstract classes provide the ability for core implementations.
Listing 4 – IMyClass Interface converted to an Abstract Class
JustCode Steps: “Convert Interface to abstract class”
Pull members to Base Types
Moves a member from a derived class into its base class. Listing 5 – Class with added method
Listing 6 – Method added to abstract base class
JustCode Steps: “Pull Members to Base Type…”