Skip to content

C++ Design Patterns in KBEngine ​

This is an external-engine learning reference. Pattern names describe ways to organize responsibilities; they do not define Apollo's process topology or certify that a particular feature is implemented. See Apollo architecture status.

Patterns and responsibilities ​

PatternExample responsibilityBenefitTradeoff
SingletonProcess-wide application accessA common access pointHidden dependencies and harder isolation in tests
FactoryEntity construction by registered typeDecouples callers from concrete entity typesRegistration and lifetime errors must be visible
ObserverChange notification and monitoringSeparates producers from consumersUnsubscription, callback lifetime, and reentrancy require care
ProxyA local reference representing a remote entityHides location-dependent invocation mechanicsTransport success does not establish business completion
CommandMessage handlers and queued intentionsSupports controlled dispatch and execution orderRequires validation, correlation, and bounded queues
Object poolReuse of frequently allocated objectsCan reduce allocation overheadReset correctness and retention limits matter
BridgeNetwork interface and transport implementationKeeps abstraction and implementation independentAdds interfaces that need clear ownership
Template methodShared lifecycle with application hooksKeeps startup and execution order consistentInheritance can create implicit ordering constraints
StrategyPlacement and load-balancing decisionsAllows policies to vary behind one interfacePolicies still need shared admission invariants
Chain of responsibilityStaged message validation and routingKeeps processing stages focusedDropped or multiply processed requests need explicit outcomes
CompositeHierarchical runtime ownershipRepresents collections and contained units uniformlyOwnership does not imply distributed authority

How to evaluate a pattern ​

A useful review starts with ownership and failure behavior rather than a pattern label. Identify who constructs and destroys an object, which thread may mutate it, whether callbacks can outlive their target, and how a failure reaches the caller.

For network-facing code, separate decoding, authorization, routing, and simulation execution. A queued command needs a request identity and an overload result. A remote proxy needs timeout and retry semantics. An observer must not let a slow consumer block the authoritative simulation loop.

For persistent state, distinguish accepted work from durable work. A callback indicating queue insertion is not proof that a reward survived a restart. Retries require business idempotency even when transport correlation works correctly.

Applying the lessons to Apollo ​

Apollo's reusable host and module lifecycle provide the common runtime boundary. baseServer owns persistent-player gameplay; worldServer manages Space/Cell and executors; cell owns simulation/Instance; database owns storage access; loginServer hosts authentication/directories; gateway holds connections; machined supervises local processes. These responsibilities remain stable regardless of whether their implementation uses factories, observers, strategies, or composition.

Ghost/proxy concepts from KBEngine's distributed spatial model are not a requirement for Apollo's explicit cross-World transfer. Likewise, a manager strategy does not justify adding another business process when the same responsibility can be hosted by an existing application.

Prefer a small interface whose ownership and outcomes can be tested. Introduce pooling after measurement, keep policy separate from mechanism, and avoid process-wide mutable access where dependency injection makes the dependency explicit.

For the extended worked examples, see the Chinese design-pattern reference. For the relationship between source areas and process responsibilities, see KBEngine source analysis.

Released under the MIT License