Swift Is a Protocol Oriented Programming Language

Datetime:2016-08-23 04:14:20          Topic: Swift           Share

WWDC 2015 is where Protocol Oriented Programming (POP) is officially being preached.

It gave us a different perspective of programming, one that does NOT use class and inheritance .

Instead, POP use struct and protocol .

The Problem with Object Oriented Programming Language

The problem is that in practice, using class inheritance is at times not correct .

Or not flexible.

Because with inheritance, the only structure you can make is a hierarchy . You are limited by that.

But with protocols, you are essentially giving your object traits.

With protocol extension for the trait, you can give it a default trait.

It makes resuable code, much more resuable (:

To build a component, you just need to specify these individual traits, like how building blocks should be!

An Example

We look at an example of a login view controller, with a password text field.

class LoginViewController: UIViewController {
  let passwordValidator = PasswordValidator()

  @IBAction func loginButtonPressed() {
    if passwordValidator.isPasswordValid(passwordTextField.text!) {

The above use a decompostion pattern, with a PasswordValidator object.

This is nice and clean, as we put the validation logic in an object of it’s own.

Here is how you can improve it with POP, with a default implementation.

protocol ValidatesPassword {
  func isPasswordValid(password: String) -> Bool

extension ValidatesPassword {
  func isPasswordValid(password: String) -> Bool {
    // A default implementation

The validation logic is still a separate object, but now in a protocol extension.

To use,

class LoginViewController: UIViewController, ValidatesPassword {
  @IBAction func loginButtonPressed() {
    if isPasswordValid(passwordTextField.text!) {

It is now even cleaner!

You can call isPasswordValid because of ValidatesPassword , the protocol which your view controller now has.

About List