Favor composition over inheritance — in Swift

Application design still has importance, no matter which “fun” framework you want to learn this week

Steven Curtis
5 min readSep 9, 2019

Object-oriented langages have been accused of a lack of reusability. The accusation is that there is an implicit exnvironment that they carry around them. A possible solution to this is protocol conformance, and this Medium post will cover this particular topic.

Prerequisites:

  • Use of the Mirror API for reflection
  • Some understanding of OOP is useful

Terminology

Composition: A fundamental OO concept, where a class is described that reference one or more object of other classes in instance variables.

Interface: A programming structure that allows properties to be exposed as a public API.

Inheritance: A fundamental OO concept that enables new objects to take on the properties of existing objects. A class that inherits from a superclass is called a subclass or derived class.

Mixin: A special kind of multiple inheritance. Is a trait with its own properties, and since property values are stored in the concrete class rather than the protocol we can say that Swift does not fully support…

--

--