Skip to content
Katabench
Try free

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.

Your job

Replace the false promise with two honest, nongeneric capabilities:

  • IMembership represents the behavior every membership genuinely shares.
  • IRenewableMembership represents memberships that can accept more months. It may extend IMembership because every renewable membership is still a membership.
  • MonthlyMembership is a sealed renewable membership. LifetimeMembership is a separate, sealed membership and is not renewable. Neither concrete type inherits the old Membership base.
  • RenewalService is sealed, accepts only IRenewableMembership, stores its dependency in a readonly field, and delegates renewal without inspecting concrete types.
  • Solution is the composition root. It chooses the lifetime display workflow or wires a monthly membership into RenewalService.

Keep the public behavior unchanged. Monthly inputs return months:<n> after renewal. Lifetime inputs return lifetime without pretending a lifetime membership can renew.

The decoy

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.

How it is graded

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.

SOLID principles exercises →

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