Make the Membership Contract Honest
Hard About 40 min Free, no account needed
An abstract Membership promises renewal even though lifetime memberships cannot renew, forcing RenewalService to recognize a concrete subtype before it can safely use the base contract. Split shared membership identity from the renewable capability, remove the lying inheritance, and make the service depend only on what it can honestly call.
The brief
The abstract Membership base promises that every membership can be renewed. A
MonthlyMembership honors that promise. A LifetimeMembership cannot, so its implementation
throws. The application avoids the exception only because RenewalService recognizes the
concrete lifetime subtype and refuses to call the method the base class says is safe.
The output is correct, but the contract is not. Code that accepts a Membership cannot use the
whole contract without first discovering which concrete object it was given. That breaks the
Liskov Substitution Principle: a subtype is not substitutable when callers must know its identity
to avoid part of the base API.
Replace the false promise with two honest, nongeneric capabilities:
IMembershiprepresents the behavior every membership genuinely shares.IRenewableMembershiprepresents memberships that can accept more months. It may extendIMembershipbecause every renewable membership is still a membership.MonthlyMembershipis a sealed renewable membership.LifetimeMembershipis a separate, sealed membership and is not renewable. Neither concrete type inherits the oldMembershipbase.RenewalServiceis sealed, accepts onlyIRenewableMembership, stores its dependency in a readonly field, and delegates renewal without inspecting concrete types.Solutionis the composition root. It chooses the lifetime display workflow or wires a monthly membership intoRenewalService.
Keep the public behavior unchanged. Monthly inputs return months:<n> after renewal. Lifetime
inputs return lifetime without pretending a lifetime membership can renew.
Adding IMembership and IRenewableMembership to the old class hierarchy is not the repair. If
both concrete classes still inherit Membership, they still inherit its false renewal promise.
Making the old base implement the new interfaces merely gives the lie two more names. The
no-old-base rules catch both inheritance edges, and the lifetime rule rejects any path from
LifetimeMembership to IRenewableMembership. The trusted seam also checks that the lifetime
type exposes no public Renew operation, so renaming the dishonest base does not preserve its lie.
Keeping the type guard is the other decoy. A service that accepts Membership and checks is LifetimeMembership still depends on the dishonest contract and on a concrete exception to its
own policy. The dependency rules reject both references. The behavioral seam then replaces the
monthly membership with a trusted fake whose result is unrelated to the real month count. Only a
service that can be constructed with the renewable capability and actually calls it can surface
the fake's sentinel.
Your submission must pass all five behavior cases and every architecture rule. The rules pin the
two contracts, each concrete role, the removed inheritance edges, the narrow service dependency,
readonly storage, and sealed classes. The seam separately proves that RenewalService uses the
renewable object it receives instead of keeping the interface for show, and that
LifetimeMembership has no public renewal operation.
A trustworthy abstraction describes what every value behind it can do. When only some values support an operation, model that operation as a capability callers must request explicitly.
How it is graded
Executable structural checks grade the shape of your types as well as their behavior.
What it practices
- Liskov substitution
- Subtypes must honor every promise their base type makes.
- Interface segregation
- Split fat interfaces so clients depend only on what they use.
Related katas
- Program to the Abstraction Hard Free An IPaymentProcessor abstraction exists, but CardProcessor only duck-types it and a second implementer, LegacyCharger, honours it under the wrong name. Wiring up the obvious processor clears the implements rule but leaves the role's naming contract red. Make every payment method a true, conventionally-named drop-in over a proper abstract base.
- Shrink the Machine Interface Medium Free One fat IMachine interface forces the inkjet printer to fake a Scan it cannot perform, and the print queue to demand a scanner it never uses. Split the roles into IPrinter and IScanner. Keeping IMachine around as a composed sub-interface leaves the inkjet transitively welded to scanning and two rules red.
- Split the God Port Hard Pro One fat warehouse interface serves a consumer that only reads and a consumer that also reserves, so reporting can see a method it must never call. Splitting the interface is not enough: each half needs its own published namespace, and the behavior tests force one implementation behind both.
- A Rule Tree Can Contain Any Rule Hard Free Replace a central tagged-tree evaluator with recursive rule objects that preserve child order, empty identities, and short-circuit behavior.