Knowledgebase

Protocols and Protocol-Oriented Design Print

  • appletechnology, apple, troubleshooting, guide, howto, solution, zillionkinghost, hosting
  • 0

Contracts over inheritance.

WHAT A PROTOCOL IS

A set of requirements a type can declare it satisfies.

WHAT THAT PROVIDES

A contract without an implementation.

WHY IT MATTERS

Code depends on capability rather than a concrete type, which makes substitution and testing possible.

WHAT PROTOCOL EXTENSIONS ADD

Default implementations, shared by every conforming type.

WHY THAT IS SIGNIFICANT

It provides code sharing without inheritance, and works for value types.

WHAT COMPOSITION MEANS HERE

A type conforming to several protocols, each small and focused.

WHAT TO PREFER

Small protocols describing one capability.

WHY

Large protocols force conforming types to implement things they do not need.

WHAT ASSOCIATED TYPES PROVIDE

Protocols parameterised by a type the conforming type chooses.

WHAT THEY COMPLICATE

Using the protocol as a plain type, which requires additional handling.

WHAT THE MODERN KEYWORDS DISTINGUISH

A specific concrete type the caller need not know Any conforming type, boxed at runtime

WHAT TO PREFER

The first, where the type is fixed, since it avoids runtime cost.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot