Techniquecore
Replace subclass with delegate
Replace a subclass that holds one variation of behavior with a separate delegate object that the main class holds and calls.
- Family
- refactoring
- Intent
- Let an object vary in more than one way, and change its variation at run time.
Purpose
Subclasses are a simple way to vary behavior, but they have two limits:
- An object can vary in only one way with subclasses. If orders vary by priority and by region, one set of subclasses cannot express both.
- The class of an object is fixed. An order cannot become a priority order after it is made.
Replace subclass with delegate moves the variation into a separate object. The main class holds a reference to the delegate and calls it. The catalogue card has Order, whose daysToShip reads this._warehouse.daysToShip, and PriorityOrder extends Order, which overrides it to read this._priorityPlan.daysToShip. After the refactoring, Order asks a _priorityDelegate if it has one, and the warehouse if it does not. The class PriorityOrderDelegate holds the priority logic.1
Steps
- If callers call the subclass constructor, use Replace Constructor with Factory Function, so that the creation is in one place. See Replace Constructor with Factory Function.
- Make an empty delegate class. Its constructor takes the data of the subclass, and often a back-reference to the main object.
- Add a field for the delegate to the main class. Change the factory function to set the delegate in place of making the subclass.
- Move each method of the subclass to the delegate. In the main class, make the method call the delegate when it has one, and use its own logic when it does not. Test after each method.
- When the subclass is empty, remove it. Test.
Example
A priority order ships faster. After the refactoring, the priority logic is in a delegate, and an order can become a priority order at run time.
import assert from "node:assert/strict";
const warehouse = { daysToShip: 5 };
const priorityPlan = { daysToShip: 2 };
class OrderBefore { constructor(warehouse) { this._warehouse = warehouse; } get daysToShip() { return this._warehouse.daysToShip; } }
class PriorityOrderBefore extends OrderBefore {
constructor(warehouse, plan) { super(warehouse); this._priorityPlan = plan; }
get daysToShip() { return this._priorityPlan.daysToShip; }
}
class PriorityOrderDelegate {
constructor(host, plan) { this._host = host; this._priorityPlan = plan; }
get daysToShip() { return this._priorityPlan.daysToShip; }
}
class Order {
constructor(warehouse) { this._warehouse = warehouse; }
get daysToShip() {
return this._priorityDelegate ? this._priorityDelegate.daysToShip : this._warehouse.daysToShip;
}
makePriority(plan) { this._priorityDelegate = new PriorityOrderDelegate(this, plan); }
}
function createOrder(warehouse, plan) {
const order = new Order(warehouse);
if (plan) order.makePriority(plan);
return order;
}
assert.equal(createOrder(warehouse).daysToShip, new OrderBefore(warehouse).daysToShip);
assert.equal(createOrder(warehouse, priorityPlan).daysToShip, new PriorityOrderBefore(warehouse, priorityPlan).daysToShip);
// With a delegate, an order can become a priority order after it is made.
const order = createOrder(warehouse);
const days = [order.daysToShip];
order.makePriority(priorityPlan);
days.push(order.daysToShip);
console.log(days);
[ 5, 2 ]
| Language | What the example shows |
|---|---|
| JavaScript | PriorityOrder replaced with PriorityOrderDelegate, as above |
| Python | the same change; the delegate is an optional attribute |
| Go | the Go form: Go has no subclasses, so a delegate held in a field of interface type is the normal way to vary behavior |
from types import SimpleNamespace
warehouse = SimpleNamespace(days_to_ship=5)
priority_plan = SimpleNamespace(days_to_ship=2)
class PriorityOrderDelegate:
def __init__(self, host: "Order", plan) -> None:
self._host = host
self._plan = plan
@property
def days_to_ship(self) -> int:
return self._plan.days_to_ship
class Order:
def __init__(self, warehouse) -> None:
self._warehouse = warehouse
self._priority_delegate = None
@property
def days_to_ship(self) -> int:
if self._priority_delegate is not None:
return self._priority_delegate.days_to_ship
return self._warehouse.days_to_ship
def make_priority(self, plan) -> None:
self._priority_delegate = PriorityOrderDelegate(self, plan)
order = Order(warehouse)
days = [order.days_to_ship]
order.make_priority(priority_plan)
days.append(order.days_to_ship)
print(days)
[5, 2]
package main
import "fmt"
// shipper is the axis of variation. A delegate in a field replaces a subclass.
type shipper interface{ DaysToShip() int }
type Warehouse struct{ Days int }
func (w Warehouse) DaysToShip() int { return w.Days }
type PriorityPlan struct{ Days int }
func (p PriorityPlan) DaysToShip() int { return p.Days }
type Order struct{ shipping shipper }
func (o *Order) DaysToShip() int { return o.shipping.DaysToShip() }
func (o *Order) MakePriority(p PriorityPlan) { o.shipping = p }
func main() {
order := &Order{shipping: Warehouse{Days: 5}}
days := []int{order.DaysToShip()}
order.MakePriority(PriorityPlan{Days: 2})
days = append(days, order.DaysToShip())
fmt.Println(days)
}
[5 2]
When to use
- More than one axis of variation. Use subclasses for one axis at most, and delegates for the others.
- The variation changes at run time. A field can point to a new delegate. A class cannot change.
- The superclass and subclass are in different modules. A change to a superclass can break a subclass in another module. A delegate has a smaller interface with the main class.
See Composition over inheritance.
Pitfalls
More code. A delegate needs a class, a field and a call in each method that varies. For one stable axis of variation, a subclass is simpler.
The back-reference. A delegate that needs data of the main object holds a reference to it. The two classes then depend on each other. Keep the delegate's use of the host small.
Many delegated methods. If each method of the main class starts with a test for the delegate, use a default delegate for the normal case, so that the main class always calls a delegate. This is the Strategy pattern.
Tools that do it
No IDE has one action for this refactoring. The steps are done by hand.
The catalogue lists the refactoring in the second edition only, with the tag dealing-with-inheritance.2 In the second edition it is in Chapter 12, Dealing with Inheritance.3
References
- Martin Fowler, Catalog of Refactorings, Replace Subclass with Delegate, the entry, the Order and PriorityOrder sketch
- Martin Fowler, Catalog of Refactorings, index, the entry replaceSubclassWithDelegate, its books and tags
- Martin Fowler, Refactoring, Improving the Design of Existing Code, second edition, Addison-Wesley, 2018, Chapter 12, Dealing with Inheritance, Replace Subclass with Delegate
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag YJR11W Edit this page History Report an error Markdown