Skip to content

Instantly share code, notes, and snippets.

@trikitrok
trikitrok / AdaptParameter.ts
Last active March 20, 2025 23:47
Adapt Parameter initial code: we don't own the interface AcmeInputReader (it's an Acme's interface)
class Library {
private readonly display: Display;
constructor(display: Display) {
this.display = display;
}
// we don't want to use a test double of an interface we don't own
printBooks(books: Book[], inputReader: AcmeInputReader): void {
const libraryName = inputReader.readLine();
class Library {
private display: Display;
constructor(display: Display) {
this.display = display;
}
printBooks(books: Book[]): void {
const inputReader = new ConsoleInputReader(); // creating the ConsoleInputReader here impedes unit testing
const libraryName = inputReader.readLine();
// We'd like to test PaydayTransaction which
// depends on a concrete class: TransactionLog
class TransactionLog {
public saveTransaction(paydayTransaction: PaydayTransaction): void {
// persist the transaction
// ...
}
}
@trikitrok
trikitrok / wrap-class.md
Created March 14, 2025 10:08
Wrap class

Wrap class.

How to do it:

  1. Identify the change point.

  2. Create a class that accepts the class we are going to wrap as a constructor argument.

    • If we have trouble creating the original class in a test harness, we might have to use Extract Interface on the wrapped class so that we can instantiate the wrapper.
  3. Create a method on the wrapper class with tests, that implements the new behaviour.

@trikitrok
trikitrok / sprout-class.md
Created March 14, 2025 10:02
Sprout class

Sprout class.

How to do it:

  1. Identify the change point and determine the sprouted class interface.

    • Pass to the sprouted class’s constructor any source method’s local variables it needs as parameters.

    • Determine whether the sprouted class’ method will need to return values to the source method.

@trikitrok
trikitrok / wrap-method.md
Created March 14, 2025 10:00
Wrap method

Wrap method.

Version 1 (when clients will only use the composed behaviour).

How to do it:

  1. Identify the change point.

  2. Extract a method with the body of the current method.

@trikitrok
trikitrok / sprout-method.md
Last active March 14, 2025 10:02
Sprout method

Sprout method.

How to do it:

  1. Identify the change point and determine the sprouted method signature (input/output).

    • Pass to the sprouted method any source method’s local variables it needs as parameters.

    • Determine whether the sprouted method will need to return values to any variable in the source method.

<?php
interface Customer {
public function getEarnedDiscount(): float;
public function addToOrdersHistory(Order $order): void;
}
// Null Customer's implementation
class NotFoundCustomer implements Customer {
public interface Customer
{
double GetEarnedDiscount();
void AddToOrdersHistory(Order order);
}
// Null Customer's implementation
public class NotFoundCustomer : Customer
{
public interface Customer {
double getEarnedDiscount();
void addToOrdersHistory(Order order);
}
// Null Customer's implementation
public class NotFoundCustomer implements Customer {
private final double DEFAULT_DISCOUNT = 1.0;