[swift-users] Comparing POP to OOP

Daniel Tartaglia danielt1263 at gmail.com
Mon Feb 15 12:52:09 CST 2016


That’s easy to do by allowing an Animal to hold multiple modes. Yes, the code below uses a Protocol, but only as an OO interface.

let alligator = Animal()
alligator.mode.append(Land())
alligator.mode.append(Sea())

protocol Mode {
    func attack() -> Bool
    func move() -> Bool
}

class Animal {
    var modes: [Mode]
    func attack() -> Bool {
        for mode in modes {
            if mode.attack() {
                break
            }
        }
    }
}



> On Feb 15, 2016, at 1:43 PM, Jon Hoffman <hoffman.jon at gmail.com> wrote:
> 
> Thank you for the feedback however you cannot design the code as you describe, if I understand your explanation correctly, because one of the requirements is the animals may be apart of multiple categories.  As the example in the post shows the alligator belongs to both the Land and the Sea categories.  In you description that would mean that the Alligator type would need to be a subclass of both the Land and Sea superclasses which is not permitted.  Remember that one of the drawbacks with OOP is a subclass can only inherit from one superclass.
> 
> Jon
> 
> On Mon, Feb 15, 2016 at 1:17 PM, Daniel Tartaglia <danielt1263 at gmail.com <mailto:danielt1263 at gmail.com>> wrote:
> (Reposting because I forgot to change the subject line. Hope that this is the correct thing to do.)
> 
> I have to say John that I am not a fan of your OOP code. I would have written the OOP code very much like you wrote the POP version using the Strategy pattern.
> 
> [Animal]<*>--->[Mode]
>                   ^
>                   |
>            +------+------+
>            |      |      |
>         [Land]  [Sea]  [Air]
>      
> 
> (View the above with a mono-spaced font.)
> 
> In essence, I see no difference. There may be a difference, but I don’t think your example presents one.
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160215/b4e8e73e/attachment.html>


More information about the swift-users mailing list