This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5429279-programming-in-swift-functions-and-types/lessons/45
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5429279-programming-in-swift-functions-and-types/lessons/45
At 5:56, what is Composition?
Hi! Good question, and my apologies for not really explaining that in the video.
There is a common phrase that you may hear: “Use composition over inheritance”. It’s an object-oriented programming principle that basically encourages you to build your custom types out of many smaller, focused types, instead of building massive superclasses or complicated inheritance chains. In this case, because Swift types can conform to more than one protocol, you could say that types can be “composed” of many protocols. So protocols allow you to favor “composition over inheritance.”
1 Like