Programming Search⌘K

Encapsulation refactorings

The refactorings of chapter 7 that hide data and decisions behind interfaces.

Techniques

  • Encapsulate collection

    Stop a getter from handing out the collection that an object owns; give the object methods to add and remove elements, and return a copy or a read-only view.

    core
  • Encapsulate record

    Replace a plain record, such as a dictionary or a JSON object, with an object whose accessors are the only way to reach the data, so that the storage can change and the callers do not.

    core
  • Extract class

    Split a class that does two jobs into two classes, by moving the fields and methods of one job into a class of its own and keeping a reference to it.

    core
  • Hide delegate

    Give a class a method that forwards to an object it holds, so that its clients ask the class and do not reach through it to the object behind; the inverse of Remove Middle Man.

    core
  • Inline class

    Move all the fields and methods of a class that does too little into the class that uses it, and delete the class; the inverse of Extract Class.

    core
  • Remove middle man

    When a class does little but forward calls to an object it holds, let clients call that object directly and delete the forwarding methods; the inverse of Hide Delegate.

    core
  • Replace primitive with object

    Wrap a string or a number that stands for a concept of the domain, such as an email address or an amount of money, in a small class that holds the rules of that concept.

    core
  • Replace temp with query

    Replace a local variable that holds a computed value with a function that computes it, so that other functions can ask for the value and the long function gets easier to split.

    core
  • Substitute algorithm

    Replace the body of a function with a clearer algorithm that gives the same results, and use the old body as the test oracle while you switch.

    core