Protocol oriented programming vs OOP in swift [on hold]

I’ve just started to used some protocol oriented programming features. For example i can write something like this:

protocol Monster {
    var name: String { get set }
}

extension Monster {
    func destroy(city:String) {
        print("(name) destroyed (city)")
    }
}

struct PlatedMonster: Monster {
    var name = ""
}

struct FlyingMonster: Monster {
    var name = ""
}

this gives the advantages to use the same implementation across 2 structs with it’s something that you can’t do without the Protocol oriented programming.
What it’s not clear it’s:

  1. Why i would write structs as in the example above instead of using 2 classes and use the classic inheritance?


Source: oop

Leave a Reply