SlideShare a Scribd company logo
1 of 31
Download to read offline
< Capability Driven Design >
Andrzej Jóźwiak
“If you make information
available someone will make
use of it”
David Parnas
class Mail {
fun send() {
// business logic ...
}
// or
fun receive() {
// business logic ...
}
}
fun main() {
val email = Mail()
email.send()
// vs
email.receive()
}
class Foo {
fun initialize() {
// should be called before using the class
}
fun release() {
// should be called after using the class
}
// some other important methods here...
}
fun main() {
val foo = Foo()
// although we've created Foo we cannot use it :(
// but nothing in this world prevents us from it
foo.initialize()
// now we can use it without a sudden runtime exception
// as soon as we release Foo we cannot use it :(
foo.release()
// but nothing in this world prevents us from it
}
class MaybeABetterFoo {
var initialized = false
private set
fun initialize(): Boolean {
if (!initialized) {
// do something
}
return initialized
}
fun release(): Boolean {
if (initialized) {
// do something
}
return initialized
}
// other important methods here ...
}
val foo = MaybeABetterFoo()
if (foo.initialized) {
// we are paranoid and check before we do anything
}
if (foo.initialize()) {
// did we manage to initialize it?
}
if (foo.release()) {
// did we manage to release it?
}
class MaybeABetterFoo {
// … //
fun canDoSth(): Boolean =
TODO("A very important logic here!")
fun doSth() {}
}
val foo = MaybeABetterFoo()
if (foo.initialize()) {
// if we managed to initialize it then do something else
if (foo.initialized) {
// we often need to double check if it was not released
if (foo.canDoSth()) {
// maybe there are still things that need to be checked
foo.doSth() // our code looks like an arrow >
}
}
}
class BarUninitialized {
fun initialize(): BarInitialized =
BarInitialized()
// other things that can be done with uninitialized Bar
}
class BarInitialized {
fun release(): BarUninitialized =
BarUninitialized()
// other things that can be done with initialized Bar
fun doSomething() {
}
}
Instead of asking what CAN’T be done ask about
what CAN!
class GameState {
// Returns all possible move capabilities for the given state
fun getAvailableCapabilities(): List<MoveCapabilities>
// Uses one of the capabilities for a given player
fun run(player: Player, capability: MoveCapabilities)
// Check the status of the game
fun getGameResult(): GameResult
}
sealed class MoveCapabilities {
data class PLAY_CARDS(val cards: List<Card>) : MoveCapabilities()
data class DRAW_CARDS(val amount: Int) : MoveCapabilities()
object END_TURN : MoveCapabilities()
object FORFEIT_GAME : MoveCapabilities()
}
Modify the state with a picked capability, get
new state with a new set of capabilities.
sealed class Capabilities(val move: () -> BetterGameState) {
class PLAY_CARDS(
val cards: List<Card>,
move: () -> BetterGameState
) : Capabilities(move)
class DRAW_CARDS(
val amount: Int,
move: () -> BetterGameState
) : Capabilities(move)
class END_TURN(
move: () -> BetterGameState
) : Capabilities(move)
class FORFEIT_GAME(
move: () -> BetterGameState
) : Capabilities(move)
}
class BetterGameState {
// Returns all possible move capabilities for the given state
fun getAvailableCapabilities(): List<Capabilities>
// Check the status of the game
fun getGameResult(): GameResult
fun getCurrentPlayer(): Player
}
fun main() {
val game = BetterGameState()
val capabilities = game.getAvailableCapabilities()
// we can present the options to the player
// let's assume the player picked one of the capabilities:
capabilities[0].move() // <-- running the capability
// player info is baked in, we cannot do an invalid move
}
Are you sure your users are not ?
class UserAcquisitionManager
(
private val db: Database /*, other fields of course */
) {
// super important business logic that is using the database
}
// some database
class Database {
fun run(query: DbQuery): DbResult =
DbResult() // some dummy result
}
// represents a possible query in the database
class DbQuery
// represents a possible results when doing a query on the
database
class DbResult
class UserAcquisitionManager
(
private val query: (DbQuery) -> DbResult /*, other fields of course */
) {
// super important business logic that is using the database
}
fun log(tag: String, message: String) =
println("$tag:${generateTimestamp()}:$message")
fun generateTimestamp(): Long = 1L
fun <A, B> addLogging(tag: String, f: (A) -> B): (A) -> B = {
log(tag, "$it")
f(it)
}
fun main() {
val db = Database()
val queryWithLogging: (DbQuery) -> DbResult =
addLogging("UserAcquisitionManager", db::run)
val uam = UserAcquisitionManager(queryWithLogging)
}
interface Revoker {
fun revoke() // for the sake of the example this is good enough
}
fun <A, B> revocable(f: (A) -> B): Pair<(A) -> B, Revoker> {
val available = AtomicBoolean(true)
val revocableFunction: (A) -> B = {
if (available.get()) {
f(it)
}
throw IllegalStateException("Privileges were revoked!")
}
val revoker = object : Revoker {
override fun revoke() {
available.set(false)
}
}
return Pair(revocableFunction, revoker)
}
val db = Database()
val revocable: Pair<(query: DbQuery) -> DbResult, Revoker> =
revocable(db::run)
val revocableFunction: (query: DbQuery) -> DbResult =
revocable.first
val revoker: Revoker =
revocable.second
val uam = UserAcquisitionManager(revocableFunction)
// now to revoke at any moment
revoker.revoke()
// we could wrap everything with logging:
val loggedRevocableFunction: (query: DbQuery) -> DbResult =
addLogging("UserAcquisitionManager", revocableFunction)
// or
val revocableLogged: Pair<(query: DbQuery) -> DbResult, Revoker> =
revocable(addLogging("UserAcquisitionManager", db::run))
val revocableLoggedFunction: (query: DbQuery) -> DbResult =
revocable.first
val revokerLogged: Revoker =
revocable.second
// we can make things better with:
class UserAcquisitionManagerBetter
(
private val query: (DbQuery) -> (() -> DbResult)? /*, other fields */
) {
// super important business logic that is using the database
}
// or … even better:
class UserAcquisitionManagerEvenBetter
(
private val query: () -> DbResult /*, here other fields of course */
) {
// super important business logic that is using the database
}
Designing with Capabilities
Domain Modeling Made
Functional
Functional Design Patterns
F# for Fun and Profit
(much more …)
Functional Architecture – the pits
of success
Types + Properties = Software
Type Driven Development
Ploeh blog
(much more …)
Thank you!

More Related Content

What's hot

Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeBuilding Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeMongoDB
 
Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeBuilding Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeStripe
 
스위프트를 여행하는 히치하이커를 위한 스타일 안내
스위프트를 여행하는 히치하이커를 위한 스타일 안내스위프트를 여행하는 히치하이커를 위한 스타일 안내
스위프트를 여행하는 히치하이커를 위한 스타일 안내Jung Kim
 
Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeBuilding Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeMongoDB
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixturesBill Chang
 
Introducción rápida a SQL
Introducción rápida a SQLIntroducción rápida a SQL
Introducción rápida a SQLCarlos Hernando
 
Javascript ES6 generators
Javascript ES6 generatorsJavascript ES6 generators
Javascript ES6 generatorsRamesh Nair
 
10b. Graph Databases Lab
10b. Graph Databases Lab10b. Graph Databases Lab
10b. Graph Databases LabFabio Fumarola
 
Backbone.js: Run your Application Inside The Browser
Backbone.js: Run your Application Inside The BrowserBackbone.js: Run your Application Inside The Browser
Backbone.js: Run your Application Inside The BrowserHoward Lewis Ship
 
History of jQuery
History of jQueryHistory of jQuery
History of jQueryjeresig
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data ObjectsWez Furlong
 
Implementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with SlickImplementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with SlickHermann Hueck
 
Writing Your App Swiftly
Writing Your App SwiftlyWriting Your App Swiftly
Writing Your App SwiftlySommer Panage
 
Cycle.js: Functional and Reactive
Cycle.js: Functional and ReactiveCycle.js: Functional and Reactive
Cycle.js: Functional and ReactiveEugene Zharkov
 
Unbreakable: The Craft of Code
Unbreakable: The Craft of CodeUnbreakable: The Craft of Code
Unbreakable: The Craft of CodeJoe Morgan
 
ES6 patterns in the wild
ES6 patterns in the wildES6 patterns in the wild
ES6 patterns in the wildJoe Morgan
 

What's hot (20)

Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeBuilding Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at Stripe
 
Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeBuilding Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at Stripe
 
스위프트를 여행하는 히치하이커를 위한 스타일 안내
스위프트를 여행하는 히치하이커를 위한 스타일 안내스위프트를 여행하는 히치하이커를 위한 스타일 안내
스위프트를 여행하는 히치하이커를 위한 스타일 안내
 
Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeBuilding Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at Stripe
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixtures
 
ES6 in Real Life
ES6 in Real LifeES6 in Real Life
ES6 in Real Life
 
Introducción rápida a SQL
Introducción rápida a SQLIntroducción rápida a SQL
Introducción rápida a SQL
 
Javascript ES6 generators
Javascript ES6 generatorsJavascript ES6 generators
Javascript ES6 generators
 
10b. Graph Databases Lab
10b. Graph Databases Lab10b. Graph Databases Lab
10b. Graph Databases Lab
 
Backbone.js: Run your Application Inside The Browser
Backbone.js: Run your Application Inside The BrowserBackbone.js: Run your Application Inside The Browser
Backbone.js: Run your Application Inside The Browser
 
Php 101: PDO
Php 101: PDOPhp 101: PDO
Php 101: PDO
 
Elm: give it a try
Elm: give it a tryElm: give it a try
Elm: give it a try
 
History of jQuery
History of jQueryHistory of jQuery
History of jQuery
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
 
Implementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with SlickImplementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with Slick
 
Academy PRO: ES2015
Academy PRO: ES2015Academy PRO: ES2015
Academy PRO: ES2015
 
Writing Your App Swiftly
Writing Your App SwiftlyWriting Your App Swiftly
Writing Your App Swiftly
 
Cycle.js: Functional and Reactive
Cycle.js: Functional and ReactiveCycle.js: Functional and Reactive
Cycle.js: Functional and Reactive
 
Unbreakable: The Craft of Code
Unbreakable: The Craft of CodeUnbreakable: The Craft of Code
Unbreakable: The Craft of Code
 
ES6 patterns in the wild
ES6 patterns in the wildES6 patterns in the wild
ES6 patterns in the wild
 

Similar to Capability Driven Design - Andrzej Jóźwiak - TomTom Dev Day 2021

Capability Driven Design - Rapid Talks - November 2020
Capability Driven Design - Rapid Talks - November 2020Capability Driven Design - Rapid Talks - November 2020
Capability Driven Design - Rapid Talks - November 2020Andrzej Jóźwiak
 
jQuery: out with the old, in with the new
jQuery: out with the old, in with the newjQuery: out with the old, in with the new
jQuery: out with the old, in with the newRemy Sharp
 
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6Dmitry Soshnikov
 
Implement threads and a GUI interface using advanced Java Swing clas.pdf
Implement threads and a GUI interface using advanced Java Swing clas.pdfImplement threads and a GUI interface using advanced Java Swing clas.pdf
Implement threads and a GUI interface using advanced Java Swing clas.pdfamrishinda
 
This is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdfThis is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdfanjandavid
 
No excuses, switch to kotlin
No excuses, switch to kotlinNo excuses, switch to kotlin
No excuses, switch to kotlinThijs Suijten
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdffonecomp
 
jQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journeyjQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journeyHuiyi Yan
 
Kotlin : Advanced Tricks - Ubiratan Soares
Kotlin : Advanced Tricks - Ubiratan SoaresKotlin : Advanced Tricks - Ubiratan Soares
Kotlin : Advanced Tricks - Ubiratan SoaresiMasters
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5arajivmordani
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016Manoj Kumar
 
The Future of JVM Languages
The Future of JVM Languages The Future of JVM Languages
The Future of JVM Languages VictorSzoltysek
 
TDC218SP | Trilha Kotlin - DSLs in a Kotlin Way
TDC218SP | Trilha Kotlin - DSLs in a Kotlin WayTDC218SP | Trilha Kotlin - DSLs in a Kotlin Way
TDC218SP | Trilha Kotlin - DSLs in a Kotlin Waytdc-globalcode
 

Similar to Capability Driven Design - Andrzej Jóźwiak - TomTom Dev Day 2021 (20)

Capability Driven Design - Rapid Talks - November 2020
Capability Driven Design - Rapid Talks - November 2020Capability Driven Design - Rapid Talks - November 2020
Capability Driven Design - Rapid Talks - November 2020
 
jQuery: out with the old, in with the new
jQuery: out with the old, in with the newjQuery: out with the old, in with the new
jQuery: out with the old, in with the new
 
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
 
Txjs
TxjsTxjs
Txjs
 
Unittests für Dummies
Unittests für DummiesUnittests für Dummies
Unittests für Dummies
 
Implement threads and a GUI interface using advanced Java Swing clas.pdf
Implement threads and a GUI interface using advanced Java Swing clas.pdfImplement threads and a GUI interface using advanced Java Swing clas.pdf
Implement threads and a GUI interface using advanced Java Swing clas.pdf
 
This is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdfThis is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdf
 
The Beauty of Java Script
The Beauty of Java ScriptThe Beauty of Java Script
The Beauty of Java Script
 
No excuses, switch to kotlin
No excuses, switch to kotlinNo excuses, switch to kotlin
No excuses, switch to kotlin
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
 
jQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journeyjQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journey
 
Kotlin : Advanced Tricks - Ubiratan Soares
Kotlin : Advanced Tricks - Ubiratan SoaresKotlin : Advanced Tricks - Ubiratan Soares
Kotlin : Advanced Tricks - Ubiratan Soares
 
Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5a
 
EcmaScript 6
EcmaScript 6 EcmaScript 6
EcmaScript 6
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
ES6 generators
ES6 generatorsES6 generators
ES6 generators
 
Exploring ES6
Exploring ES6Exploring ES6
Exploring ES6
 
The Future of JVM Languages
The Future of JVM Languages The Future of JVM Languages
The Future of JVM Languages
 
TDC218SP | Trilha Kotlin - DSLs in a Kotlin Way
TDC218SP | Trilha Kotlin - DSLs in a Kotlin WayTDC218SP | Trilha Kotlin - DSLs in a Kotlin Way
TDC218SP | Trilha Kotlin - DSLs in a Kotlin Way
 

More from Andrzej Jóźwiak

Encapsulation – the pitfalls of Object-Oriented Programming - Andrzej Jóźwiak...
Encapsulation – the pitfalls of Object-Oriented Programming - Andrzej Jóźwiak...Encapsulation – the pitfalls of Object-Oriented Programming - Andrzej Jóźwiak...
Encapsulation – the pitfalls of Object-Oriented Programming - Andrzej Jóźwiak...Andrzej Jóźwiak
 
Does testability imply good design - Andrzej Jóźwiak - TomTom Dev Day 2022
Does testability imply good design - Andrzej Jóźwiak - TomTom Dev Day 2022Does testability imply good design - Andrzej Jóźwiak - TomTom Dev Day 2022
Does testability imply good design - Andrzej Jóźwiak - TomTom Dev Day 2022Andrzej Jóźwiak
 
Introduction to the Kotlin programming language - Andrzej Jóźwiak - JUG Łódź ...
Introduction to the Kotlin programming language - Andrzej Jóźwiak - JUG Łódź ...Introduction to the Kotlin programming language - Andrzej Jóźwiak - JUG Łódź ...
Introduction to the Kotlin programming language - Andrzej Jóźwiak - JUG Łódź ...Andrzej Jóźwiak
 
Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...
Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...
Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...Andrzej Jóźwiak
 
Types of Randomness in Game Design - Rapid Talks - December 2020
Types of Randomness in Game Design - Rapid Talks - December 2020Types of Randomness in Game Design - Rapid Talks - December 2020
Types of Randomness in Game Design - Rapid Talks - December 2020Andrzej Jóźwiak
 
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020Andrzej Jóźwiak
 
JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...
JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...
JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...Andrzej Jóźwiak
 

More from Andrzej Jóźwiak (7)

Encapsulation – the pitfalls of Object-Oriented Programming - Andrzej Jóźwiak...
Encapsulation – the pitfalls of Object-Oriented Programming - Andrzej Jóźwiak...Encapsulation – the pitfalls of Object-Oriented Programming - Andrzej Jóźwiak...
Encapsulation – the pitfalls of Object-Oriented Programming - Andrzej Jóźwiak...
 
Does testability imply good design - Andrzej Jóźwiak - TomTom Dev Day 2022
Does testability imply good design - Andrzej Jóźwiak - TomTom Dev Day 2022Does testability imply good design - Andrzej Jóźwiak - TomTom Dev Day 2022
Does testability imply good design - Andrzej Jóźwiak - TomTom Dev Day 2022
 
Introduction to the Kotlin programming language - Andrzej Jóźwiak - JUG Łódź ...
Introduction to the Kotlin programming language - Andrzej Jóźwiak - JUG Łódź ...Introduction to the Kotlin programming language - Andrzej Jóźwiak - JUG Łódź ...
Introduction to the Kotlin programming language - Andrzej Jóźwiak - JUG Łódź ...
 
Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...
Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...
Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...
 
Types of Randomness in Game Design - Rapid Talks - December 2020
Types of Randomness in Game Design - Rapid Talks - December 2020Types of Randomness in Game Design - Rapid Talks - December 2020
Types of Randomness in Game Design - Rapid Talks - December 2020
 
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
 
JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...
JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...
JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...
 

Recently uploaded

computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxPurva Nikam
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 

Recently uploaded (20)

computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptx
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 

Capability Driven Design - Andrzej Jóźwiak - TomTom Dev Day 2021

  • 1. < Capability Driven Design > Andrzej Jóźwiak
  • 2. “If you make information available someone will make use of it” David Parnas
  • 3.
  • 4.
  • 5.
  • 6. class Mail { fun send() { // business logic ... } // or fun receive() { // business logic ... } } fun main() { val email = Mail() email.send() // vs email.receive() }
  • 7. class Foo { fun initialize() { // should be called before using the class } fun release() { // should be called after using the class } // some other important methods here... } fun main() { val foo = Foo() // although we've created Foo we cannot use it :( // but nothing in this world prevents us from it foo.initialize() // now we can use it without a sudden runtime exception // as soon as we release Foo we cannot use it :( foo.release() // but nothing in this world prevents us from it }
  • 8. class MaybeABetterFoo { var initialized = false private set fun initialize(): Boolean { if (!initialized) { // do something } return initialized } fun release(): Boolean { if (initialized) { // do something } return initialized } // other important methods here ... }
  • 9. val foo = MaybeABetterFoo() if (foo.initialized) { // we are paranoid and check before we do anything } if (foo.initialize()) { // did we manage to initialize it? } if (foo.release()) { // did we manage to release it? }
  • 10. class MaybeABetterFoo { // … // fun canDoSth(): Boolean = TODO("A very important logic here!") fun doSth() {} } val foo = MaybeABetterFoo() if (foo.initialize()) { // if we managed to initialize it then do something else if (foo.initialized) { // we often need to double check if it was not released if (foo.canDoSth()) { // maybe there are still things that need to be checked foo.doSth() // our code looks like an arrow > } } }
  • 11. class BarUninitialized { fun initialize(): BarInitialized = BarInitialized() // other things that can be done with uninitialized Bar } class BarInitialized { fun release(): BarUninitialized = BarUninitialized() // other things that can be done with initialized Bar fun doSomething() { } }
  • 12. Instead of asking what CAN’T be done ask about what CAN!
  • 13. class GameState { // Returns all possible move capabilities for the given state fun getAvailableCapabilities(): List<MoveCapabilities> // Uses one of the capabilities for a given player fun run(player: Player, capability: MoveCapabilities) // Check the status of the game fun getGameResult(): GameResult } sealed class MoveCapabilities { data class PLAY_CARDS(val cards: List<Card>) : MoveCapabilities() data class DRAW_CARDS(val amount: Int) : MoveCapabilities() object END_TURN : MoveCapabilities() object FORFEIT_GAME : MoveCapabilities() }
  • 14. Modify the state with a picked capability, get new state with a new set of capabilities.
  • 15. sealed class Capabilities(val move: () -> BetterGameState) { class PLAY_CARDS( val cards: List<Card>, move: () -> BetterGameState ) : Capabilities(move) class DRAW_CARDS( val amount: Int, move: () -> BetterGameState ) : Capabilities(move) class END_TURN( move: () -> BetterGameState ) : Capabilities(move) class FORFEIT_GAME( move: () -> BetterGameState ) : Capabilities(move) }
  • 16. class BetterGameState { // Returns all possible move capabilities for the given state fun getAvailableCapabilities(): List<Capabilities> // Check the status of the game fun getGameResult(): GameResult fun getCurrentPlayer(): Player }
  • 17. fun main() { val game = BetterGameState() val capabilities = game.getAvailableCapabilities() // we can present the options to the player // let's assume the player picked one of the capabilities: capabilities[0].move() // <-- running the capability // player info is baked in, we cannot do an invalid move }
  • 18. Are you sure your users are not ?
  • 19. class UserAcquisitionManager ( private val db: Database /*, other fields of course */ ) { // super important business logic that is using the database }
  • 20. // some database class Database { fun run(query: DbQuery): DbResult = DbResult() // some dummy result } // represents a possible query in the database class DbQuery // represents a possible results when doing a query on the database class DbResult
  • 21. class UserAcquisitionManager ( private val query: (DbQuery) -> DbResult /*, other fields of course */ ) { // super important business logic that is using the database }
  • 22. fun log(tag: String, message: String) = println("$tag:${generateTimestamp()}:$message") fun generateTimestamp(): Long = 1L fun <A, B> addLogging(tag: String, f: (A) -> B): (A) -> B = { log(tag, "$it") f(it) }
  • 23. fun main() { val db = Database() val queryWithLogging: (DbQuery) -> DbResult = addLogging("UserAcquisitionManager", db::run) val uam = UserAcquisitionManager(queryWithLogging) }
  • 24. interface Revoker { fun revoke() // for the sake of the example this is good enough } fun <A, B> revocable(f: (A) -> B): Pair<(A) -> B, Revoker> { val available = AtomicBoolean(true) val revocableFunction: (A) -> B = { if (available.get()) { f(it) } throw IllegalStateException("Privileges were revoked!") } val revoker = object : Revoker { override fun revoke() { available.set(false) } } return Pair(revocableFunction, revoker) }
  • 25. val db = Database() val revocable: Pair<(query: DbQuery) -> DbResult, Revoker> = revocable(db::run) val revocableFunction: (query: DbQuery) -> DbResult = revocable.first val revoker: Revoker = revocable.second val uam = UserAcquisitionManager(revocableFunction) // now to revoke at any moment revoker.revoke()
  • 26. // we could wrap everything with logging: val loggedRevocableFunction: (query: DbQuery) -> DbResult = addLogging("UserAcquisitionManager", revocableFunction) // or val revocableLogged: Pair<(query: DbQuery) -> DbResult, Revoker> = revocable(addLogging("UserAcquisitionManager", db::run)) val revocableLoggedFunction: (query: DbQuery) -> DbResult = revocable.first val revokerLogged: Revoker = revocable.second
  • 27. // we can make things better with: class UserAcquisitionManagerBetter ( private val query: (DbQuery) -> (() -> DbResult)? /*, other fields */ ) { // super important business logic that is using the database } // or … even better: class UserAcquisitionManagerEvenBetter ( private val query: () -> DbResult /*, here other fields of course */ ) { // super important business logic that is using the database }
  • 28. Designing with Capabilities Domain Modeling Made Functional Functional Design Patterns F# for Fun and Profit (much more …)
  • 29. Functional Architecture – the pits of success Types + Properties = Software Type Driven Development Ploeh blog (much more …)
  • 30.