Clean iOS Architecture 101: Composition over Inheritance
Articles Blog

Clean iOS Architecture 101: Composition over Inheritance

October 25, 2019


Caio: Why should we prefer Composition over Inheritance? Or why is Composition better than Inheritance? Well this principle saying “prefer Composition over Inheritance” it actually has a name. Well this principle saying “prefer Composition over Inheritance” it actually has a name. In Object Oriented Programming we call it “The Composite Reuse Principle.” And composing objects is often judged better than Inheritance… Because in most languages including Swift… You can’t inherit from multiple classes. You can only inherit from one. Thus, you can’t compose with Inheritance. You can’t achieve class composition with Inheritance. So if you want to compose types and create reusable tiny single-purpose components… Inheritance is not going to help you do that. Mike: Yes, but we have Protocols! Caio: Yes, and Protocols compose much better! Because you can inherit from multiple protocols. And protocols can also inherit from multiple protocols. And since you have protocol extensions… Where you can give protocol implementations… You can compose functionality with Swift protocols.

Only registered users can comment.

  1. Get the free guide with the foundation behind all career strategies we use to help iOS developers boost their income and work with remarkable people… https://www.essentialdeveloper.com/how-to-boost-your-ios-developer-income

Leave a Reply

Your email address will not be published. Required fields are marked *