Skip to content

Instantly share code, notes, and snippets.

@fgroeger
Created May 16, 2018 05:46
Show Gist options
  • Save fgroeger/708b8cd4d5aa7c7a7b77bb894decf026 to your computer and use it in GitHub Desktop.
Save fgroeger/708b8cd4d5aa7c7a7b77bb894decf026 to your computer and use it in GitHub Desktop.

SOLID

I - Interface Segregation

An Interface should have one purpose and one purpose only. If a class wants to implement a functionality from an interface, it shouldn't have to implement methods it doesn't need.

Example from Apple's UIKit:

protocol UITableViewDataSource: class {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

    optional func numberOfSections(in tableView: UITableView) -> Int
}

A class implementing UITableViewDataSource is needed for every UITableView to provide content which will be displayed in the TableView.

protocol UITableViewDelegate: class {
        
    optional func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    optional func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
} 

UITableViewDelegate provides functionality to handle specific events of the UITableView.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment