SlideShare a Scribd company logo
1 of 108
Protocol Oriented Programming
Jayant Jaiswal, iOS Engineer, UrbanClap
#SwiftMeetup
What is it, in the first place ?
What is it, in the first place ?
Protocol Oriented Programming is a paradigm based around
the concept of Protocol Extensions.
Design a View Controller
Configurable Navigation Bar
Animation like show and hide loader
Loading from xib
A common approach
Design a base View Controller that gets inherited to all
those controllers that share the similarity.
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
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
Drawbacks
Drawbacks
Reusability, if same piece of code is required somewhere else
Drawbacks
Hard to navigate between classes and fix bugs, due to nested code
Reusability, if same piece of code is required somewhere else
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
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
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
UIKit Framework in OOP
UIKit Framework in OOP
Would you ever like to work with the above code base?
Protocol Oriented Design to rescue
Protocol Oriented Design to rescue
Separated out functionalities making them reusable
Protocol Oriented Design to rescue
Separated out functionalities making them reusable
Flat Structured, non-nested code
Protocol Oriented Design to rescue
Separated out functionalities making them reusable
Flat Structured, non-nested code
Easier to debug and fix bugs
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
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
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
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
A better solution
A better solution
A better solution
A better solution
A better solution
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
Add a functionality over Integers to convert them to string
Add a functionality over Integers to convert them to string
A common approach
Add a functionality over Integers to convert them to string
A common approach
Add a functionality over Integers to convert them to string
A common approach
Do the same for Float
Add a functionality over Integers to convert them to string
A common approach
Do the same for Float
Add a functionality over Integers to convert them to string
A common approach
Do the same for Float
A better approach using POP
A better approach using POP
A better approach using POP
A better approach using POP
A better approach using POP
A better approach using POP
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
Design a View Controller
Should be able to show a pop up
Should be able to configure right bar button item
The SuperClass Way
The SuperClass Way
Redesigning using Protocol Oriented Programming
Redesigning using Protocol Oriented Programming
Redesigning using Protocol Oriented Programming
Using the Newly designed Protocols
Using the Newly designed Protocols
Using the Newly designed Protocols
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 { }
Task 1
Calling the above method would result in 7 and 123a respectively
Problem:
Both the situations
are possible
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
Heterogenous Containers
Problem:
Both the situations
are possible
Heterogenous Containers
Heterogenous Containers
Heterogenous Containers
Heterogenous Containers
Constraint
Each element conforms to Summable
Homogeneous Containers
Homogeneous Containers
Homogeneous Containers
Advantages of Homogeneous over Heterogeneous
Containers
Advantages of Homogeneous over Heterogeneous
Containers
The tedious type check has been eliminated that we had to do in the
heterogenous case.
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.
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
Constraining Protocol Extensions With Types
Constraining Protocol Extensions With Types
public protocol ReusableView : class {
static var cellReuseIdentifier : String { get }
}
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)
}
}
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 ?
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)
}
}
How Swift Team used this trick in the Swift Standard library ?
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
}
}
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 == ?
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
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
}
}
Task 2
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
protocol BasePower {
init( )
}
class Pokemon <Power : BasePower> {
func attack( ) -> Power {
return Power( )
}
}
Protocol Associated Types
protocol BasePower {
init( )
}
class Pokemon <Power : BasePower> {
func attack( ) -> Power {
return Power( )
}
}
Protocol Associated Types
Power Types
struct WaterPower : BasePower { }
struct FirePower : BasePower { }
struct LighteningPower : BasePower { }
Protocol Associated Types
class Pikachu : Pokemon<LighteningPower> { }
class Squirtle : Pokemon<WaterPower> { }
class Charizard : Pokemon<FirePower> { }
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( )
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.
protocol Pokemon {
associatedtype Power : BasePower
func attack( ) -> Power
}
Protocol Associated Types
extension Pokemon {
func attack( ) -> Power {
return Power( )
}
}
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( )
}
}
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
let pokemon: Pokemon
pokemon.attack( )
Type Erasures
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
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.
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( )
}
}
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( )
}
}
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( )
}
}
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( )
}
}
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( )
}
}
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
Thank You

More Related Content

What's hot

Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageHossam Ghareeb
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLsintelliyole
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ Ganesh Samarthyam
 
Introduction to c_sharp
Introduction to c_sharpIntroduction to c_sharp
Introduction to c_sharpHEM Sothon
 
Lambda/Streams Hands-On Lab
Lambda/Streams Hands-On LabLambda/Streams Hands-On Lab
Lambda/Streams Hands-On LabMaurice Naftalin
 
Test-driven language development
Test-driven language developmentTest-driven language development
Test-driven language developmentlennartkats
 
How does intellisense work?
How does intellisense work?How does intellisense work?
How does intellisense work?Adam Friedman
 
Language Engineering in the Cloud
Language Engineering in the CloudLanguage Engineering in the Cloud
Language Engineering in the Cloudlennartkats
 
Programming Paradigms
Programming ParadigmsProgramming Paradigms
Programming ParadigmsJaneve George
 
Building scalable and language independent java services using apache thrift
Building scalable and language independent java services using apache thriftBuilding scalable and language independent java services using apache thrift
Building scalable and language independent java services using apache thriftTalentica Software
 

What's hot (20)

Kotlin
KotlinKotlin
Kotlin
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming language
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
 
C sharp
C sharpC sharp
C sharp
 
Api and Fluency
Api and FluencyApi and Fluency
Api and Fluency
 
2CPP02 - C++ Primer
2CPP02 - C++ Primer2CPP02 - C++ Primer
2CPP02 - C++ Primer
 
C Course Material0209
C Course Material0209C Course Material0209
C Course Material0209
 
Introduction to c_sharp
Introduction to c_sharpIntroduction to c_sharp
Introduction to c_sharp
 
Research paper on python by Rj
Research paper on python by RjResearch paper on python by Rj
Research paper on python by Rj
 
Cucumber_Training_ForQA
Cucumber_Training_ForQACucumber_Training_ForQA
Cucumber_Training_ForQA
 
scope of python
scope of pythonscope of python
scope of python
 
Lambda/Streams Hands-On Lab
Lambda/Streams Hands-On LabLambda/Streams Hands-On Lab
Lambda/Streams Hands-On Lab
 
Test-driven language development
Test-driven language developmentTest-driven language development
Test-driven language development
 
How does intellisense work?
How does intellisense work?How does intellisense work?
How does intellisense work?
 
Language Engineering in the Cloud
Language Engineering in the CloudLanguage Engineering in the Cloud
Language Engineering in the Cloud
 
Cucumber in Practice(en)
Cucumber in Practice(en)Cucumber in Practice(en)
Cucumber in Practice(en)
 
Programming Paradigms
Programming ParadigmsProgramming Paradigms
Programming Paradigms
 
Building scalable and language independent java services using apache thrift
Building scalable and language independent java services using apache thriftBuilding scalable and language independent java services using apache thrift
Building scalable and language independent java services using apache thrift
 
Introduction to refactoring
Introduction to refactoringIntroduction to refactoring
Introduction to refactoring
 

Similar to Protocol oriented programming_talk_ppt

Evolutionary Design Solid
Evolutionary Design SolidEvolutionary Design Solid
Evolutionary Design SolidSai Venkat
 
CommonJS via PINF JavaScript Loader - Introduction
CommonJS via PINF JavaScript Loader - IntroductionCommonJS via PINF JavaScript Loader - Introduction
CommonJS via PINF JavaScript Loader - Introductioncadorn
 
CASE tools and their effects on software quality
CASE tools and their effects on software qualityCASE tools and their effects on software quality
CASE tools and their effects on software qualityUtkarsh Agarwal
 
Beyond design patterns phpnw14
Beyond design patterns   phpnw14Beyond design patterns   phpnw14
Beyond design patterns phpnw14Anthony Ferrara
 
Intro to Muon - How to build Polyglot Message and Event Microservices
Intro to Muon - How to build Polyglot Message and Event MicroservicesIntro to Muon - How to build Polyglot Message and Event Microservices
Intro to Muon - How to build Polyglot Message and Event MicroservicesDavid Dawson
 
Docker Enables DevOps - Keep C.A.L.M.S. and Docker on ...
Docker Enables DevOps - Keep C.A.L.M.S. and Docker on ...Docker Enables DevOps - Keep C.A.L.M.S. and Docker on ...
Docker Enables DevOps - Keep C.A.L.M.S. and Docker on ...Boyd Hemphill
 
Lecture: Refactoring
Lecture: RefactoringLecture: Refactoring
Lecture: RefactoringMarcus Denker
 
NDC Sydney 2019 - Microservices for building an IDE – The innards of JetBrain...
NDC Sydney 2019 - Microservices for building an IDE – The innards of JetBrain...NDC Sydney 2019 - Microservices for building an IDE – The innards of JetBrain...
NDC Sydney 2019 - Microservices for building an IDE – The innards of JetBrain...Maarten Balliauw
 
Keep calms and Docker On ... Innotech
Keep calms and Docker On ... InnotechKeep calms and Docker On ... Innotech
Keep calms and Docker On ... InnotechBoyd Hemphill
 
ConFoo Montreal - Microservices for building an IDE - The innards of JetBrain...
ConFoo Montreal - Microservices for building an IDE - The innards of JetBrain...ConFoo Montreal - Microservices for building an IDE - The innards of JetBrain...
ConFoo Montreal - Microservices for building an IDE - The innards of JetBrain...Maarten Balliauw
 
iOS Development at Scale @Chegg
iOS Development at Scale @CheggiOS Development at Scale @Chegg
iOS Development at Scale @CheggGalOrlanczyk
 
Resource Discovery Landscape
Resource Discovery LandscapeResource Discovery Landscape
Resource Discovery LandscapeAndy Powell
 
WordCamp Pune 2017- WordPress Coding standards
WordCamp Pune 2017- WordPress Coding standardsWordCamp Pune 2017- WordPress Coding standards
WordCamp Pune 2017- WordPress Coding standardsSwapnil Patil
 
Migrating 25K lines of Ant scripting to Gradle
Migrating 25K lines of Ant scripting to GradleMigrating 25K lines of Ant scripting to Gradle
Migrating 25K lines of Ant scripting to Gradle🎤 Hanno Embregts 🎸
 

Similar to Protocol oriented programming_talk_ppt (20)

Evolutionary Design Solid
Evolutionary Design SolidEvolutionary Design Solid
Evolutionary Design Solid
 
CommonJS via PINF JavaScript Loader - Introduction
CommonJS via PINF JavaScript Loader - IntroductionCommonJS via PINF JavaScript Loader - Introduction
CommonJS via PINF JavaScript Loader - Introduction
 
Evolutionary Design Solid
Evolutionary Design SolidEvolutionary Design Solid
Evolutionary Design Solid
 
Hello Cotrix
Hello CotrixHello Cotrix
Hello Cotrix
 
CASE tools and their effects on software quality
CASE tools and their effects on software qualityCASE tools and their effects on software quality
CASE tools and their effects on software quality
 
Beyond design patterns phpnw14
Beyond design patterns   phpnw14Beyond design patterns   phpnw14
Beyond design patterns phpnw14
 
Intro to Muon - How to build Polyglot Message and Event Microservices
Intro to Muon - How to build Polyglot Message and Event MicroservicesIntro to Muon - How to build Polyglot Message and Event Microservices
Intro to Muon - How to build Polyglot Message and Event Microservices
 
Docker Enables DevOps - Keep C.A.L.M.S. and Docker on ...
Docker Enables DevOps - Keep C.A.L.M.S. and Docker on ...Docker Enables DevOps - Keep C.A.L.M.S. and Docker on ...
Docker Enables DevOps - Keep C.A.L.M.S. and Docker on ...
 
Lecture: Refactoring
Lecture: RefactoringLecture: Refactoring
Lecture: Refactoring
 
NDC Sydney 2019 - Microservices for building an IDE – The innards of JetBrain...
NDC Sydney 2019 - Microservices for building an IDE – The innards of JetBrain...NDC Sydney 2019 - Microservices for building an IDE – The innards of JetBrain...
NDC Sydney 2019 - Microservices for building an IDE – The innards of JetBrain...
 
Java basics
Java basicsJava basics
Java basics
 
Pa2 session 4
Pa2 session 4Pa2 session 4
Pa2 session 4
 
Keep calms and Docker On ... Innotech
Keep calms and Docker On ... InnotechKeep calms and Docker On ... Innotech
Keep calms and Docker On ... Innotech
 
Codeigniter
CodeigniterCodeigniter
Codeigniter
 
ConFoo Montreal - Microservices for building an IDE - The innards of JetBrain...
ConFoo Montreal - Microservices for building an IDE - The innards of JetBrain...ConFoo Montreal - Microservices for building an IDE - The innards of JetBrain...
ConFoo Montreal - Microservices for building an IDE - The innards of JetBrain...
 
Illustrated Code (ASE 2021)
Illustrated Code (ASE 2021)Illustrated Code (ASE 2021)
Illustrated Code (ASE 2021)
 
iOS Development at Scale @Chegg
iOS Development at Scale @CheggiOS Development at Scale @Chegg
iOS Development at Scale @Chegg
 
Resource Discovery Landscape
Resource Discovery LandscapeResource Discovery Landscape
Resource Discovery Landscape
 
WordCamp Pune 2017- WordPress Coding standards
WordCamp Pune 2017- WordPress Coding standardsWordCamp Pune 2017- WordPress Coding standards
WordCamp Pune 2017- WordPress Coding standards
 
Migrating 25K lines of Ant scripting to Gradle
Migrating 25K lines of Ant scripting to GradleMigrating 25K lines of Ant scripting to Gradle
Migrating 25K lines of Ant scripting to Gradle
 

Recently uploaded

CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual serviceanilsa9823
 
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPowerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPsychicRuben LoveSpells
 
9892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x79892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x7Pooja Nehwal
 
Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Night 7k to 12k Top Call Girls Ahmedabad 👉 BOOK NOW 8617697112 👈 ♀️ night gir...
Night 7k to 12k Top Call Girls Ahmedabad 👉 BOOK NOW 8617697112 👈 ♀️ night gir...Night 7k to 12k Top Call Girls Ahmedabad 👉 BOOK NOW 8617697112 👈 ♀️ night gir...
Night 7k to 12k Top Call Girls Ahmedabad 👉 BOOK NOW 8617697112 👈 ♀️ night gir...Call girls in Ahmedabad High profile
 
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun serviceanilsa9823
 
Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...
Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...
Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...Niamh verma
 
哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...
哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...
哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...wyqazy
 
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,Pooja Nehwal
 

Recently uploaded (9)

CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
 
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPowerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
 
9892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x79892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x7
 
Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝
 
Night 7k to 12k Top Call Girls Ahmedabad 👉 BOOK NOW 8617697112 👈 ♀️ night gir...
Night 7k to 12k Top Call Girls Ahmedabad 👉 BOOK NOW 8617697112 👈 ♀️ night gir...Night 7k to 12k Top Call Girls Ahmedabad 👉 BOOK NOW 8617697112 👈 ♀️ night gir...
Night 7k to 12k Top Call Girls Ahmedabad 👉 BOOK NOW 8617697112 👈 ♀️ night gir...
 
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
 
Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...
Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...
Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...
 
哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...
哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...
哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...
 
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
 

Protocol oriented programming_talk_ppt

  • 1. Protocol Oriented Programming Jayant Jaiswal, iOS Engineer, UrbanClap #SwiftMeetup
  • 2. What is it, in the first place ?
  • 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
  • 9. Drawbacks Reusability, if same piece of code is required somewhere else
  • 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
  • 15. UIKit Framework in OOP Would you ever like to work with the above code base?
  • 17. Protocol Oriented Design to rescue Separated out functionalities making them reusable
  • 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
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 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
  • 37. Add a functionality over Integers to convert them to string
  • 38. Add a functionality over Integers to convert them to string A common approach
  • 39. Add a functionality over Integers to convert them to string A common approach
  • 40. Add a functionality over Integers to convert them to string A common approach Do the same for Float
  • 41. Add a functionality over Integers to convert them to string A common approach Do the same for Float
  • 42. Add a functionality over Integers to convert them to string A common approach Do the same for Float
  • 43. A better approach using POP
  • 44. A better approach using POP
  • 45. A better approach using POP
  • 46. A better approach using POP
  • 47. A better approach using POP
  • 48. A better approach using POP
  • 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
  • 53. Redesigning using Protocol Oriented Programming
  • 54. Redesigning using Protocol Oriented Programming
  • 55. Redesigning using Protocol Oriented Programming
  • 56.
  • 57.
  • 58. Using the Newly designed Protocols
  • 59. Using the Newly designed Protocols
  • 60. Using the Newly designed Protocols
  • 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 { }
  • 62. Task 1 Calling the above method would result in 7 and 123a respectively
  • 63.
  • 64.
  • 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
  • 67. Heterogenous Containers Problem: Both the situations are possible
  • 75. Advantages of Homogeneous over Heterogeneous Containers
  • 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
  • 91. protocol BasePower { init( ) } class Pokemon <Power : BasePower> { func attack( ) -> Power { return Power( ) } } Protocol Associated Types
  • 92. protocol BasePower { init( ) } class Pokemon <Power : BasePower> { func attack( ) -> Power { return Power( ) } } Protocol Associated Types Power Types struct WaterPower : BasePower { } struct FirePower : BasePower { } struct LighteningPower : BasePower { }
  • 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.
  • 96. protocol Pokemon { associatedtype Power : BasePower func attack( ) -> Power } Protocol Associated Types extension Pokemon { func attack( ) -> Power { return Power( ) } }
  • 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