In one sentence

Define the skeleton of the algorithm in an operation, deferring some steps to subclass implementation. The template method allows subclasses to redefine specific steps of an algorithm without changing its structure.

Code

The ViewController in iOS is the classic case of the template method pattern.

class CustomViewController: UIViewController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
    }
    
    override func viewDidLoad(a) {
        super.viewDidLoad()
    }
}
Copy the code