Terminology (6)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Dynamic dispatch
- Choosing the implementation from the value while the program runs: an override, a virtual function, an interface or trait object.
- Static dispatch
- Choosing the implementation at compile time from the declared or generic type.
- Override
- A subclass method that replaces the base version for calls dispatched on the object.
- Embedding (Go)
- Including a type as an unnamed field, which promotes its methods; they still run on the inner value.
- Composition
- Reusing code by holding another value and forwarding calls to it.
- Fragile base class
- A subclass that breaks when the base changes how its own methods call each other.
Selecting and reusing behavior
A receiver identifies the value a method uses; dispatch selects the implementation for a call. Static selection uses compile-time type information, while dynamic dispatch can select behavior from the runtime value. The call form matters as well as the method name.
Inheritance reuses behavior through a base relationship. Composition holds a collaborator and delegates selected work to it. In either design, follow internal calls as well as the entry call: reused code may reach a helper on its own receiver or forward to a different value.
Go embedding promotes selectors for a contained field. It does not introduce subclass overriding: a promoted method retains the embedded value as receiver. Interface and constrained generic calls use the methods supplied by their concrete values.
01Who chooses
The compiler, or the running program
A coworking subscription desk quotes monthly workspace plans and logs the visitor badges issued at check-in. Standing workspaces add a configured 50 surcharge to the 200 base price. The badge log records single scans and whole batches as transactions; undo removes the last transaction, so its count must follow the entries that actually remain.
Before repairing plan quotes, use a small claim calculator to isolate dispatch from rental arithmetic. The meal claim holds 45 and caps payout at 30; follow each general-type or generic call and predict which implementation runs.
package main
import "fmt"
type Claim interface{ Payout() int }
type Meal struct {
Amount int
}
func (m Meal) Payout() int { return min(m.Amount, 30) }
func payStatic[T Claim](claim T) int { return claim.Payout() }
func payDynamic(claim Claim) int { return claim.Payout() }
func main() {
meal := Meal{Amount: 45}
fmt.Println(payStatic(meal), payDynamic(meal))
}
go run main.go
Use the setup from the first reading for your language.
02Inherited methods
Which version a shared method calls
A reused bill can call another method internally. This claim-summary probe isolates that second hop: shared summary calls limit, while the hotel claim supplies 120. Predict which helper implementation the shared body reaches.
package main
import "fmt"
type Claim struct{}
func (Claim) Limit() int { return 30 }
func (c Claim) Summary() string { return fmt.Sprintf("limit %d", c.Limit()) }
type HotelClaim struct {
Claim
}
func (HotelClaim) Limit() int { return 120 }
func main() {
fmt.Println(HotelClaim{}.Summary())
}
03Composition
Forwarding runs the held value’s own methods
Badge counters will also forward work to a held log. First use a simpler claim wrapper: it has its own limit of 50 but delegates summary to a held basic claim. Predict what that specific forwarding path reports.
package main
import "fmt"
type Claim struct{}
func (Claim) Limit() int { return 30 }
func (c Claim) Summary() string { return fmt.Sprintf("limit %d", c.Limit()) }
type CappedClaim struct {
inner Claim
}
func (CappedClaim) Limit() int { return 50 }
func (c CappedClaim) Summary() string { return c.inner.Summary() }
func main() {
fmt.Println(CappedClaim{inner: Claim{}}.Summary())
}
04Repair the subscription desk
Quote the selected plan, reconcile badge batches, then undo a transaction
First quote each selected desk plan for its rental months using that plan’s monthly price. Then reconcile the visitor badge count with both single and batch scans. Finally undo the last recorded transaction: a batch undo removes every badge in that batch, while a single undo removes one.
The count summarizes the badges retained in the log. The undo version supplies a transaction-grouped Log, so inspect its actual method bodies again when moving from batch counting to history. The supplied single/batch/undo witnesses exercise calls that dispatch back into a specialized method and calls forwarded to another object. Each native has its own mechanism; inheritance syntax alone does not establish the contract.
Repair the subscription desk
Which version runs, a base that calls back, then a wrapper that keeps its own state.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?