Protocol Oriented Programming in

2017-08-21  本文已影响50人  mobilefellow

下列内容是摘录Reference的文章而生成的读书笔记。

Protocol v.s. Base Class

Protocol v.s. Simple Category/Extension

Protocol makes codes more readable and organizable. And it also provides more access control, since functions in category/extension are accessable for class instances.

Let’s say your product manager comes over and says, “We want a view that when you click a button, it starts shaking.” This is a very common animation with password fields for example – when the user enters the wrong password, it might shake. So we can use the UI extension below to add shake to all UIViews.

extension UIView {

    func shake() {
        let animation = CABasicAnimation(keyPath: "position")
        animation.duration = 0.05
        animation.repeatCount = 5
        animation.autoreverses = true
        animation.fromValue = NSValue(CGPoint: CGPointMake(self.center.x - 4.0, self.center.y))
        animation.toValue = NSValue(CGPoint: CGPointMake(self.center.x + 4.0, self.center.y))
        layer.addAnimation(animation, forKey: "position")
    }
}

If you have done this much for categories and extending UI views, you might have had better experience. It becomes this Frankenstein garbage place where you start adding a shake and then someone comes and says, “We want a dimmable view”. Then you add a dim function and then there is some other random function. It becomes this long, unreadable, hard-to-find garbage file with all these random things that UI view could do, but only maybe one or two things need to do that.

It is not clear what the intention is. How can we change this?

This is a talk about protocol-oriented programming, so we are going to use protocols. Let’s create a Shakeable protocol:

protocol Shakeable { }

extension Shakeable where Self: UIView {

    func shake() {
        // implementation code
    }
}

With protocol extensions, you can constrain them to specific classes. In this case, I can take out my shake function, and, with the category, I can say that only things that conform to this protocol, only UI views, will have this function.

Similarly, we could create the following protocol.

protocol ReusableView: class {}

extension ReusableView where Self: UIView {

    static var reuseIdentifier: String {
        return String(self)
    }
}
protocol NibLoadableView: class { }

extension NibLoadableView where Self: UIView {

    static var nibName: String {
        return String(self)
    }
}

References

How Protocol Oriented Programming in Swift saved my day?

Beyond Crusty: Real-World Protocols

Raywenderlich: Introducing Protocol-Oriented Programming in Swift 3

Practical Protocol-Oriented-Programming

上一篇 下一篇

猜你喜欢

热点阅读