3. What is it, in the first place ?
Protocol Oriented Programming is a paradigm based around
the concept of Protocol Extensions.
4. Design a View Controller
Configurable Navigation Bar
Animation like show and hide loader
Loading from xib
5. A common approach
Design a base View Controller that gets inherited to all
those controllers that share the similarity.
6. A common approach
Design a base View Controller that gets inherited to all
those controllers that share the similarity.
This approach works but has
some drawbacks
7. A common approach
Design a base View Controller that gets inherited to all
those controllers that share the similarity.
This approach works but has
some drawbacks
10. Drawbacks
Hard to navigate between classes and fix bugs, due to nested code
Reusability, if same piece of code is required somewhere else
11. Drawbacks
Inherit unneeded properties and methods, making the object become bloated
Hard to navigate between classes and fix bugs, due to nested code
Reusability, if same piece of code is required somewhere else
12. Drawbacks
Inherit unneeded properties and methods, making the object become bloated
Hard to navigate between classes and fix bugs, due to nested code
Reusability, if same piece of code is required somewhere else
It is said, OOP is just a
modularized way of writing
spaghetti code
13. Drawbacks
Inherit unneeded properties and methods, making the object become bloated
Hard to navigate between classes and fix bugs, due to nested code
Reusability, if same piece of code is required somewhere else
It is said, OOP is just a
modularized way of writing
spaghetti code
18. Protocol Oriented Design to rescue
Separated out functionalities making them reusable
Flat Structured, non-nested code
19. Protocol Oriented Design to rescue
Separated out functionalities making them reusable
Flat Structured, non-nested code
Easier to debug and fix bugs
20. Protocol Oriented Design to rescue
Separated out functionalities making them reusable
Flat Structured, non-nested code
Easier to debug and fix bugs
Works for both reference and value types - classes, enums and structs
21. Protocol Oriented Design to rescue
Separated out functionalities making them reusable
Flat Structured, non-nested code
Easier to debug and fix bugs
Works for both reference and value types - classes, enums and structs
Let’s see them in action
22. Our Agenda Moving Ahead
What protocol Extensions are ?
How to think the Protocol Extension Way ?
How we can convert Object Oriented Design to its equivalent
Protocol Oriented with a cleaner design and Reusability?
Homogeneous and Heterogeneous Containers
Constraining Protocol Extensions with types
Protocol Associated Types(PATs)
Type Erasures
23. Our Agenda Moving Ahead
What protocol Extensions are ?
How to think the Protocol Extension Way ?
How we can convert Object Oriented Design to its equivalent
Protocol Oriented with a cleaner design and Reusability?
Homogeneous and Heterogeneous Containers
Constraining Protocol Extensions with types
Protocol Associated Types(PATs)
Type Erasures
36. Our Agenda Moving Ahead
What protocol Extensions are ?
How to think the Protocol Extension Way ?
How we can convert Object Oriented Design to its equivalent
Protocol Oriented with a cleaner design and Reusability?
Homogeneous and Heterogeneous Containers
Constraining Protocol Extensions with types
Protocol Associated Types(PATs)
Type Erasures
49. Our Agenda Moving Ahead
What protocol Extensions are ?
How to think the Protocol Extension Way ?
How we can convert Object Oriented Design to its equivalent
Protocol Oriented with a cleaner design and Reusability?
Homogeneous and Heterogeneous Containers
Constraining Protocol Extensions with types
Protocol Associated Types(PATs)
Type Erasures
50. Design a View Controller
Should be able to show a pop up
Should be able to configure right bar button item
61. A simple example of how the Swift Team has used
Protocol Extensions
public protocol Sequence {
//Some come
public func map<T>(_ transform: (Self.Element) throws -> T ) rethrows -> [T]
}
extension Sequence {
public func map<T>(_ transform: (Self.Element) throws -> T ) rethrows -> [T] {
//Implementation by Swift Team
}
}
extension Array: Sequence { }
extension ArraySlice: Sequence { }
extension ReversedCollection: Sequence { }
66. Our Agenda Moving Ahead
What protocol Extensions are ?
How to think the Protocol Extension Way ?
How we can convert Object Oriented Design to its equivalent
Protocol Oriented with a cleaner design and Reusability?
Homogeneous and Heterogeneous Containers
Constraining Protocol Extensions with types
Protocol Associated Types(PATs)
Type Erasures
76. Advantages of Homogeneous over Heterogeneous
Containers
The tedious type check has been eliminated that we had to do in the
heterogenous case.
77. Advantages of Homogeneous over Heterogeneous
Containers
The tedious type check has been eliminated that we had to do in the
heterogenous case.
Eliminated the need to do dynamic time type checking and replaced it with safer
compile time type assertion.
78. Our Agenda Moving Ahead
What protocol Extensions are ?
How to think the Protocol Extension Way ?
How we can convert Object Oriented Design to its equivalent
Protocol Oriented with a cleaner design and Reusability?
Homogeneous and Heterogeneous Containers
Constraining Protocol Extensions with types
Protocol Associated Types(PATs)
Type Erasures
80. Constraining Protocol Extensions With Types
public protocol ReusableView : class {
static var cellReuseIdentifier : String { get }
}
81. Constraining Protocol Extensions With Types
public protocol ReusableView : class {
static var cellReuseIdentifier : String { get }
}
extension ReusableView {
public static var cellReuseIdentifier : String {
return String(describing: self)
}
}
82. Constraining Protocol Extensions With Types
public protocol ReusableView : class {
static var cellReuseIdentifier : String { get }
}
extension ReusableView {
public static var cellReuseIdentifier : String {
return String(describing: self)
}
}
Would the above extension really make any sense if we conform a String, Int
or any other type to this protocol apart from UIView ?
83. Constraining Protocol Extensions With Types
public protocol ReusableView : class {
static var cellReuseIdentifier : String { get }
}
extension ReusableView where Self : UIView {
public static var cellReuseIdentifier : String {
return String(describing: self)
}
}
84. How Swift Team used this trick in the Swift Standard library ?
85. How Swift Team used this trick in the Swift Standard library ?
extension CollectionType {
public func index(of element: Generator.Element ) -> Index? {
for i in self.indices {
if self[ i ] == element {
return i
}
}
return nil
}
}
86. How Swift Team used this trick in the Swift Standard library ?
extension CollectionType {
public func index(of element: Generator.Element ) -> Index? {
for i in self.indices {
if self[ i ] == element {
return i
}
}
return nil
}
}
Can elements of arbitrary collections be compared with == ?
87. How Swift Team used this trick in the Swift Standard library ?
extension CollectionType {
public func index(of element: Generator.Element ) -> Index? {
for i in self.indices {
if self[ i ] == element {
return i
}
}
return nil
}
}
binary operator ‘==‘ cannot be applied to
two Generator.Element operands
88. How Swift Team used this trick in the Swift Standard library ?
extension CollectionType where Generator.Element : Equatable {
public func index(of element: Generator.Element ) -> Int? {
for i in self.indices {
if self[ i ] == element {
return i
}
}
return nil
}
}
90. Our Agenda Moving Ahead
What protocol Extensions are ?
How to think the Protocol Extension Way ?
How we can convert Object Oriented Design to its equivalent
Protocol Oriented with a cleaner design and Reusability?
Homogeneous and Heterogeneous Containers
Constraining Protocol Extensions with types
Protocol Associated Types(PATs)
Type Erasures
93. Protocol Associated Types
class Pikachu : Pokemon<LighteningPower> { }
class Squirtle : Pokemon<WaterPower> { }
class Charizard : Pokemon<FirePower> { }
94. Protocol Associated Types
class Pikachu : Pokemon<LighteningPower> { }
class Squirtle : Pokemon<WaterPower> { }
class Charizard : Pokemon<FirePower> { }
let pikachu = Pikachu( )
pikachu.attack( )
let squirtle = Squirtle( )
squirtle.attack( )
let charizard = Charizard( )
charizard.attack( )
95. Protocol Associated Types
class Pikachu : Pokemon<LighteningPower> { }
class Squirtle : Pokemon<WaterPower> { }
class Charizard : Pokemon<FirePower> { }
let pikachu = Pikachu( )
pikachu.attack( )
let squirtle = Squirtle( )
squirtle.attack( )
let charizard = Charizard( )
charizard.attack( )
Problem:
Subclassing. It starts out with great
intentions, but eventually things get
a lot messier as exceptions arise.
97. Protocol Associated Types
struct Pikachu : Pokemon {
typealias Power = LighteningPower
}
struct Squirtle : Pokemon {
// Here WaterPower is inferred as the associatedtype
func attack( ) -> WaterPower {
// custom attack logic
return WaterPower( )
}
}
98. Our Agenda Moving Ahead
What protocol Extensions are ?
How to think the Protocol Extension Way ?
How we can convert Object Oriented Design to its equivalent
Protocol Oriented with a cleaner design and Reusability?
Homogeneous and Heterogeneous Containers
Constraining Protocol Extensions with types
Protocol Associated Types(PATs)
Type Erasures
100. let pokemon: Pokemon
pokemon.attack( )
Protocol Pokemon can only be used as a generic constraint
because it has self or associated type requirements
Type Erasures
101. let pokemon: Pokemon
pokemon.attack( )
Protocol Pokemon can only be used as a generic constraint
because it has self or associated type requirements
Type Erasures
Problem:
Pokemon is an abstract type, so we cannot instantiate it directly.
102. Type Erasures
class AnyPokemon <Power>: Pokemon {
private let _attack: ( ) -> Power
required init<U: Pokemon>(_ pokemon: Pokemon)
where U.Power == Power {
_attack = pokemon.attack
}
func attack( ) -> Power {
_attack( )
}
}
103. Type Erasures
let p1 = AnyPokemon(Pikachu)
class AnyPokemon <Power>: Pokemon {
private let _attack: ( ) -> Power
required init<U: Pokemon>(_ pokemon: Pokemon)
where U.Power == Power {
_attack = pokemon.attack
}
func attack( ) -> Power {
_attack( )
}
}
104. Type Erasures
let p1 = AnyPokemon(Pikachu)
let p2: AnyPokemon<FirePower>
p2 = AnyPokemon(Charizard)
class AnyPokemon <Power>: Pokemon {
private let _attack: ( ) -> Power
required init<U: Pokemon>(_ pokemon: Pokemon)
where U.Power == Power {
_attack = pokemon.attack
}
func attack( ) -> Power {
_attack( )
}
}
105. Type Erasures
let p1 = AnyPokemon(Pikachu)
let p2: AnyPokemon<FirePower>
p2 = AnyPokemon(Charizard)
AnyPokemon<LighteningPower>
class AnyPokemon <Power>: Pokemon {
private let _attack: ( ) -> Power
required init<U: Pokemon>(_ pokemon: Pokemon)
where U.Power == Power {
_attack = pokemon.attack
}
func attack( ) -> Power {
_attack( )
}
}
106. Type Erasures
let p1 = AnyPokemon(Pikachu)
let p2: AnyPokemon<FirePower>
p2 = AnyPokemon(Charizard)
AnyPokemon<LighteningPower>
AnyPokemon<FirePower>
class AnyPokemon <Power>: Pokemon {
private let _attack: ( ) -> Power
required init<U: Pokemon>(_ pokemon: Pokemon)
where U.Power == Power {
_attack = pokemon.attack
}
func attack( ) -> Power {
_attack( )
}
}
107. Lets summarise…
How OOP can make things messier in the long run
How Protocol Oriented Design can overcome all the shortcomings
of OOPs
What are protocol extensions and how we can go about using them.
How we can go about converting from OOP to POP
Homogenous and Heterogenous Containers
Constraining extensions with types
Protocol Associated Types(PATs)
Type Erasures