Wednesday, September 26, 2018

Swift 4 Introduction Series 1.20 - Protocol and Other Features

Protocol and Other Features

When programmer share their class definition, they may implement certain protocol. Protocol are sets of methods we must implement if we were to adopt the protocol in our class.


When we use these classes and adopt the protocols, we must implement all the functions or computed property defined by the class designer.


Let's say Apple introduce a text box UI (user interface) for user to input the data. However, different programmer may have different implementation when user start typing in the field. For some very basic program, programmer may choose not to use the additional feature. Thus Apple introduce a text field delegate protocol, where programmer can define how the text field behave when user start typing into the text field.


When defining protocol, Apple decide the name of the function and what are the parameter we should use, however, the function body is empty, so that we, the programmer can implement our own coding.  When we start programming iOS apps, we will be using many protocol.


Please note that in Swift we can implement protocol in structure and class.


Define Protocol

To define protocol, we use the keyword protocol following by function definition without the function body. The syntax is as below:


protocol <protocol_name> {


// Over here we define the method/function with parameter but we do not have the function body


func <function_name>(<parameters>)
// Please note that we do not add braces since there is no body
}


Note: Please note that the syntax above is not complete. It is a simplified version. We can also implement computed properties in protocol however we will introduce computed properties in the later stage. We introduce protocol in this early stage because it is used frequently in iOS programming. We may not have the opportunity to design or define protocol, however, we will be implementing protocol very often.


This section is to provide a very basic concept and introduction to protocol so that we can start to use protocol in our iOS development.


Example:
In the following example, we as the class designer will allow programmer to provide the details of driving a boat, plane, train or car. There, we introduce a protocol known as Drive.


protocol Drive {
   
   func startEngine()
   func driveForward()
   func increaseSpeed()
   func slowDown()
   func driveBackward()
   func stopVehicle()
   func stopEngine()
   
}
 
When we design the transport vehicle class, we notice that different vehicle implementation is different. We should allow programmer to implement the detail when they see fit. For example, driving a bicycle and car is total different. Please note that the example above just illustrate the concept. We do not include climb or descent or turning to keep things simple.


Implement Protocol



Next, we would like to implement another subclass under Cars. This time we use the brand of the car as a subclass.


class Toyota: Cars, Drive {
   
   // Additional properties
   var color: UIColor
   var price: Int
   
   // Init
   override init() {
       self.color = UIColor.darkGray
       self.price = 20000
       super.init()
   }
   
   // Drive Protocol
   func startEngine() {
       
       // over here we define the specific way of starting car engine
   }
   
   func driveForward() {
   
       // we define how to step on the gas pedal
   }
   
   func increaseSpeed() {
       
       // we define how driver increase speed by changing gear and stepping on pedal
       
   }
   
   func slowDown(){
       
       // We define how driver ease the gas pedal and change the gear
   }
   
   func driveBackward() {
       
       // we define how driver change to reverse gear while stepping on the gas
   }
   
   func stopVehicle(){
       
       // we define how driver foot off pedal and stepping on the brakes
   }
   
   func stopEngine(){
       
       // we define how driver turn off the engine
   }
   

}


Please also note that when we implement a subclass with protocol, the parent class should come first follow by protocol. While implementing the subclass above, the system will keep giving us error that our class do not conform to protocol until all the function are included in this subclass.


Purpose of Using Protocol

Why should we define protocol? Protocol allow us to standardized the interface. So every transport vehicle has the same method of starting engine and increase speed, although each subclass has different implementation.


Please note that we can also allow some methods to be compulsory and some methods to be optional. These methods will be cover in advance topics.


Additional Features

In addition to the inheritance features, there are many more important features in programming class and objects.


We know that programming classes allows us to organize all the properties and methods in the form of class and objects in order to simulate the real world. Using subclassing and inheritance, we can share common class to programmers for different usage. We can add in more features (properties and methods) down the class as and when needed. We can override and re-implement methods that are created in the upper class.


In addition of these basic features, we can provide computed property. Computed property are not input by user or programmer. It is derived from some other user input. For example, we can create a property called radius for user to input their number. The area of circle (Pi x radius x radius) can be computed automatically when radius has data.


We can also set observer for the property such that it will automatically generate some code when the property changes.


We can add more functionality to an existing class even though we may not have the code of the class. We achieve this by using extension. However, we cannot override extension.


We can add protocol so as to provide a standard interface for each object.


Furthermore there are tools that allow us to compare the class of different object. We can ask an instance object to behave as the base class or to execute the specifics of the subclass. All these will be covered later.


Differences Between Structure and Class



There are differences between structure and class. But let us show the common characteristics. Both structure and class can define properties, methods, subscript,  initialization extension and protocols. Subscript is like an index similar to the subscript of an array. We will cover this in advance topics.

In addition, class instances allow inheritance and subclassing. We can also use type casting to ask an instance to behave like the base class or to behave like a subclass. Please also note that the referencing of the structure instance and class instance is different.



***

No comments:

Post a Comment