Class inheritance offers a compact specialized implementation: the scaled sum is also a
SumChannel and can be used where that concrete type is required, while its inherited state handling is reused directly.The decorator supports run-time object composition over any
Channel implementation, including an already constructed stateful software object, without editing or subclassing each concrete software class. Its extra software object and delegation are a small cost. The two approaches therefore serve different reuse needs; the wrapper need not pretend to be the concrete sum-channel software class.Use the decorator pattern. The wrapper implements the same object interface as its component and delegates before changing the output:
class ScaledChannel(inner: Channel, factor: Int) extends Channel {
override def apply(n: Int): Int = factor * inner(n)
}It can wrap any channel software object, not only a sum channel. Constructing the wrapper at run time chooses the additional behavior for an existing component, and several wrappers can be composed without adding a subclass for every combination. Each call reaches the wrapped channel once, preserving its state evolution. Scaling the input instead would define a different transformation, particularly for a component that already has history.
Feed each returned output back as the next input to the same channel software object:
class RepeatedChannel(inner: Channel, times: Int) extends Channel {
require(times > 0)
override def apply(n: Int): Int = {
var value = n
var completed = 0
while (completed < times) {
value = inner(value)
completed += 1
}
value
}
}The loop invariant says that
value and the component's current state are exactly those after completed successive applications starting from this invocation's input and the entry state. The loop variant is times - completed. On exit the specified number of applications has occurred; the component is not reset or replaced between them.For input one and a freshly created sum channel, the successive outputs areInitially the component's previous input is zero. The first output is one; on each later call its previous-input state is the input used on the preceding call. Consequently outputs follow the Fibonacci recurrence with initial outputs one and two. The answer is 89, rather than the result of repeatedly supplying the unchanged external input or recreating the component at every iteration. The component retains its final state for later wrapper calls.
Use the adapter pattern, keeping the existing component software object in both directions:
class PipeAsChannel(pipe: Pipe) extends Channel {
override def apply(n: Int): Int = {
pipe.consume(n)
pipe.result
}
}
class ChannelAsPipe(channel: Channel, initialResult: Int) extends Pipe {
private var last = initialResult
override def consume(n: Int): Unit = {
last = channel(n)
}
override def result: Int = last
}The first adapter feeds the input before reading the pipe's output. The second invokes the channel exactly once per consume and caches its returned output; subsequent result queries do not advance it. Its initial result must be supplied explicitly, because a general channel object interface exposes no initial-output query. Calling the channel on a dummy input during construction would mutate arbitrary state and would invent behavior the object interface does not promise.
These adapters preserve the sequential update/output behavior after each real input, assuming no interfering access to the wrapped component. They do not claim a perfect inverse for every aspect of the original object interfaces: pipe initialization is additional observable state, whereas a channel alone does not specify that observation.
Constructing channel implementations, decorators and adapters allocates software objects that hold state and references to other software objects. As configurations and temporary processing chains are replaced, some old software objects become unreachable. A typical Scala program on a managed runtime relies on garbage collection to recycle that storage; otherwise unreclaimed allocation can exhaust the available heap memory.
A reachable wrapper keeps its wrapped component reachable. Sharing a channel among several wrappers can extend its lifetime until all relevant references disappear. Many software objects alone do not imply that collection can free them: if the program deliberately retains every channel, those software objects remain live. The reason garbage collection is useful is repeated allocation combined with changing reachability, not an assumption that the integer-returning
apply operation allocates a fresh channel on every call.Garbage collection automatically reclaims allocated memory that is no longer reachable by the running program. Garbage collection roots such as active references and static storage lead through object-reference fields to reachable software objects in heap memory. A tracing garbage collection implementation preserves reachable software objects and can reclaim unreachable ones, including unreachable cycles.
An software object becoming unreachable is different from its state being old or its last object method call having finished. A retained reference can keep it live indefinitely. Collection timing is an implementation matter; automatic reclamation does not promise that an software object is reclaimed immediately when the final useful reference disappears.
Software object 2026-10-24
An object combines an identity, state and operations exposed through an object interface. Distinct software objects can hold equal data while remaining different by object identity. Reference aliasing occurs when multiple references designate the same object, so a state update through one reference can affect observations through another.
Structural sharing 2026-10-24
Different immutable versions reuse unchanged component software objects rather than duplicating them. Sharing a linked list suffix or unchanged subtrees saves storage while retaining distinct roots. The shared components must remain immutable for old versions to remain trustworthy. Merely sharing a mutable array does not create a stable snapshot.
Saving references to before/after versions adds constant reference storage per history entry, but the data reachable through those references may be much larger. With balanced-tree path copying, small updates to a size- structure can retain nodes. Whole-array copying can instead retain entries. The implementation and update pattern determine which bound applies.
Tracing garbage collection 2026-10-24
A tracing garbage collection implementation starts from garbage collection roots and follows software object references. Software objects outside that reachable set can be reclaimed, including cycles that have no path from a root. A decorator or adapter holding a component reference keeps that component reachable while the wrapper remains reachable. The number of allocated software objects alone does not establish how much storage can be reclaimed.