Protocol-oriented programming is an OOP paradigm that prefers the use of protocols (interfaces according to Swift terminology) and structs over classes.
The first step for new thinking in Swift should always be a protocol, Abrahams says. He spirits on then to rewrite the Ordered class example using the code and structs approach in order to show how much cleaner the ensuing implementation is:
In the snippet above, the use of Self in the precedes protocol need is what makes it available that the precedes method implementation in the Number class correctly gets the proper limit and no casting is necessary.
The protocol is a description of:
- The messages that are implicit by the object.
- The arguments that this information may be supplied with.
- The types of results that these messages return.
- The invariants that are conserved despite modifications to the state of an object.
- The exceptional situations that will be enforced to be handled by clients to the object.
Note that functional programming and distributed programming languages have a concept which is also called a protocol, but whose meaning is finally different. This difference is due to a somewhat different belief of functional programming and object-oriented programming paradigms.
Protocols really only exist to keep the compiler glad, but protocol extensions are tangible pieces of logic shared across the entire codebase.
Protocols are nothing new, but the idea that we can extend them with a built-in process, shared logic, magical power…well that’s a fascinating logic. More protocols == more flexibility. A protocol extension is a small chunk of modular functionality that can be accepted, overridden (or not) and can interact with type-specific code through the where clause.
Protocols in Swift can be generic via abstract type repetitive rather than parameterization. Consequently, the protocol itself can no longer be used as a type but only as a generic constraint.
Basic introduction of Protocol oriented Programming with swift.
Here is basic story tells to understand Protocol Oriented Programming with Swift.
Once upon a time, in the *OP kingdom, not easily far away, there was a princess called OOP.
All People have seen OOP and lots of people liked her. One day, she had a little sis, having a name POP.
People didn’t like POP. Some raise her too young, others didn’t catch when she spoke. But the truth is that people were fearful. They were afraid of the old Crusty and the old Crusty didn’t like the empress POP.
Because of that, poor princess POP always found herself alone in its very small castle at the kingdom’s boundary.
One day a brave knight of the kingdom determined to visit princess POP. Villagers suggested not to act so because the old Crusty aware on the way. “I’m not nervous of Crusty,” said the knight and he begin his journey to POP’s castle. On the way, he moves Crusty’s house but doors and shutters were closed. In fact, the old Crusty was deceased long ago.
“That’s greater,” thought the knight and he went his way. He enters at the castle and met princess POP for the 1st time.
She looked a bit like her old sister but younger, fresher and more joyful.
A party was organized to welcome the knight. He was charmed but also surprised. He was used to a celebration at OOP’s castle, but it was always very formal, very family. For example, if UIButton was the appeal to, you could be sure that his mother, UIView, would be there, and the old granny NSObject would be there as well. With POP, everything was different. UIButton could exalt with a String and an Int. The knight found the atmosphere more friendly and decided to stay a few days to get to see the princess better.
The next morning at breakfast, while they were consuming dominos and drinking Starbucks, the knight asked the princess to construct an array of the protocol without associated types. She did it with a simpler.
Then, he asked her to create an array of the protocol with associated types. She froze and reject categorically. The knight soon implicit that the princess had an evil twin.
This evil twin was called PAT and all the princess agreed to do, he refused. After a few days, the knight found himself again facing PAT. He was saddened. Maybe that’s why people didn’t like POP, maybe they had met PAT too? The knight hesitated to go ahead until another time he met PAT. That day, the knight was coped with his resume.
“I can use only one sword but I don’t know which one to choose!”, said the lover. Then, PAT came and told him ”Try this!”.
That was perfect. After that, the knight and PAT grow into BFFs. The knight decided to settle permanently at POP’s castle but he continued to usually visit OOP.
And to conclude, but it’s a hidden secret between us, he was also partying at princess FOP!