Chapter 8 — Reusing Services and Scenarios Through Role Polymorphism¶
© 2026 S++ project contributors. All rights reserved. Book copyright.
Role polymorphism allows different entities to participate in an existing business behavior through the same role contract. It reuses responsibilities without treating different natural entity types as identical.
Substitution Through a Business Contract¶
An individual and a company can both play Buyer if they provide the required identity, facts and qualifications. A purchasing scenario then consumes Buyer, not a branch for each underlying entity type. Role bindings and query mappings supply the relevant data.
Substitution is valid only when the role's semantics are satisfied. Mapping a company name into a buyer-name field is not sufficient if credit terms, eligibility, address or ownership rules differ. The role contract, not mere structural resemblance, establishes compatibility.
Reuse at Three Levels¶
- Calculation:
priceLinemultiplies an OrderLine's agreed unit price by quantity regardless of whether its Goods player is food or toys. - Validation: a quantity or payment-eligibility check evaluates role facts, not the runtime class of the buyer.
- Scenario: quotation and purchasing organize existing role-based services and can accept new players without duplicating the workflow.
The reuse boundary is the business contract. A company using the same purchasing rules mainly needs a correct Buyer relationship and fact mapping. A company requiring approval limits, credit settlement or a different discount policy changes behavior, so the relevant roles, calculations, validations or scenario steps must change explicitly.
What Polymorphism Does Not Guarantee¶
Role substitution does not automatically make two storage schemas, transaction protocols or deployment configurations equivalent. APP and PHY must still provide required facts and runtime guarantees. Nor does it eliminate domain-specific rules: adding a new kind of buyer is simpler when the contract remains stable, but a new purchasing policy is a real change in the model.
The principle is therefore precise: entities may vary while services and scenarios remain reusable to the extent that their required role contracts and behavior stay unchanged. Part II next examines how those fine-grained responsibilities can execute efficiently.
Part Contents · Full Book Contents · Previous Chapter · Next Chapter