October 2017
Intermediate to advanced
210 pages
5h 32m
English
In this section, we will demonstrate how to use the command pattern by implementing the logic for a simple calculator. To do this, we will start with a protocol that all the types that implement the math functions of the calculator must conform to. Let's name the protocol MathCommand and put the following code in it:
protocol MathCommand {
func execute(num1: Double, num2: Double) -> Double
}
The MathCommand protocol requires one function to be implemented by any type that conforms to it. This function is named execute() and takes two parameters of the Double type and returns a value also of the Double type. The types that conform to this protocol will perform some mathematical function with the two parameters. ...
Read now
Unlock full access