Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
OO in Scala - The unloved side of the cake?
Search
Tobias Neef
April 17, 2013
Programming
140
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
OO in Scala - The unloved side of the cake?
Component composition in Scala using a variant of the cake pattern
Tobias Neef
April 17, 2013
More Decks by Tobias Neef
See All by Tobias Neef
One night at a METRO depot
tobnee
0
100
Reactive Play and Scala
tobnee
1
280
Behavioural Abstractions in Play
tobnee
0
160
Funktionale Parallelität in Scala
tobnee
0
72
Functional Parallel Architecture
tobnee
1
110
Play Techtalk - Async Play
tobnee
2
530
Other Decks in Programming
See All in Programming
thread_parallel_with_free-threaded_Python_and_NumPy.pdf
riku_sakamoto
0
160
DynamoDBの基礎を振り返りながらベクトル検索機能を理解する
musan
3
270
Family mrubyの進捗
kishima
1
100
GKE アップグレード前に知っておきたい Blue/Green と PDB の関係
stkk
0
160
ハーネス設計入門 〜プロンプト、コンテキストの次〜
kinopeee
55
36k
Webプラットフォームで議論されているセキュリティ課題 / Security issues being discussed on Web Platforms
petamoriken
0
260
iOS開発×AI駆動開発 〜最近使って便利だったスキルの話〜
nogu66
0
140
マイコン向けの軽量Ruby「PicoRuby」で各種デバイスを制御するネイティブアプリの実現手法
bash0c7
0
160
From 6 People Classroom Meetup to 100 People Regional Conference / FOSS4G Hiroshima 2026
furukawayasuto
0
120
Press start. Python's next generation.
willingc
PRO
3
320
まだ間に合う!今年の夏こそSchemeのマクロ展開器を完全理解!
omasanori
0
640
PyO3 で既存 Python 評価器を Rust core 化する ー wasm-bindgen でブラウザにも配るための設計
kdash
1
560
Featured
See All Featured
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
270
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
680
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.4k
How STYLIGHT went responsive
nonsquared
100
6.3k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.8k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
290
Git: the NoSQL Database
bkeepers
PRO
432
67k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.6k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
290
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
430
A better future with KSS
kneath
240
18k
Transcript
OO in Scala The unloved side of the cake?
About Me Tobias Neef Consultant / Dev at innoQ doing
stuff in Berlin
This talk is about OO and cakes in Scala
Disclaimer I love FP but OO still has it’s values
Everybody loves cakes. Do you love the cake in Scala?
The How and Who of component composition
How
None
I
I C C C
I C C C C
I C C C C C
Who
object composition == component configuration
Who combines the objects in the required configuration to one
component?
We can solve this with DI right?
IoC
IoC Who == the container
The typical DI has some serious issues
It evolves into a add-on language
@Component public class OrderRepo { @Autowired BookingWriter bookingWriter; @Autowired OrderHeaderDao
orderHeaderDao; ...
Not covered by the typesystem
@Component public class OrderRepo { @Autowired BookingWriter bookingWriter; @Autowired OrderHeaderDao
orderHeaderDao; ...
Managed Instances
new MyClass == this is not a component
Cyclic dependencies are hard not to express
Back to the cake
Cake Pattern == Scala flavored OO component composition
Ingredients
Traits Are like interfaces with implementation
trait CustomerTS {! def getTicket(id:Long) def default = new ...
}
Self-Types Manual typing of the this reference
trait CustomerTS {! self : CustomerDao => ... }
Dependency Context Though composed traits
trait Component { def instance : ManagedClass trait ManagedClass {
def method():Int } }
trait Component { def instance : ManagedClass trait ManagedClass {
def method():Int } } Logic Context
trait Component { def instance : ManagedClass trait ManagedClass {
def method():Int } } Component Context Logic Context
trait Component { / / New Instances / / Unique
Instances / / Dependencies / / Component Inheritance }
trait ManagedClass { / / normal class / / access
to component / / application logic }
trait AuthComponent { def authComponent: Auth trait Auth { def
load(name: String, pass: String): Option[User] protected def internalUser() = .. } }
Dependencies
Class 1 Class 2 Component 1 Component 2 Depends Uses
trait BookingServiceComponent { ! self : AuthComponent => def bookingService
= ! new BookingService() class BookingService { def book(user: String, pass: String, !! itemId:Id) = ! ! ! authComponent.load(user,pass) ... } }
Inheritance
Class 1 Class 2 Component 1 Component 2 Is Uses
Extends
trait LocalAuthComponent extends AuthComponent { def authComponent = new LocalAuth()
class LocalAuth extends Auth { def load(name: String, pass: String) = ... } }
trait LDAPAuthComponent extends AuthComponent { def authComponent = new LDAPAuth()
class LDAPAuth extends Auth { def load(name: String, pass: String) = ... } }
The Application Cake
trait BookingApp extends AuthComponent BookingServiceComponent
class LiveBookingApp extends LdapAuthComponent BookingServiceComponent Dependencies are checked at compile
time
A cake component == a partial application with dependencies
Is made of the core Scala language It is checked
at compile time It requires no IoC It uses the most specific type available It can express cyclic dependencies It can’t change its configuration at runtime The Cake
Bakery of Doom
Does everything have to be a cake?
Not everything in your app is a component interface
But most of your app should belong to a component
None
None
The cake package pattern
Component Package Component Interface Internal Package Internal Component ...
packages in Scala are more than namespaces
visibility rules can shield the component internals
Your App == Functional Components / Packages Result
None
Functional and technical contexts
Functional Contexts enhance the functionality of an application
Technical Contexts are the basis for the Functional Contexts
Technical Contexts can bridge the outside world and the cake
world
trait TechnicalComponent { def tc : TechnicalContext }
trait FunctionComponent { ! this => TechnicalComponent def fc: FunctionClass
trait FunctionClass { ... } }
None
Requires
Builds Requires
Builds Requires PlayAkka ActorSystem Provides
trait GlobalActorSystem { private[core] def actorSystem: ActorSystem private[core] implicit val
defaultTimeout = Timeout(4,TimeUnit.SECONDS) } Default Implicits Technical Context
class PlayServiceModule( val actorSystem: ActorSystem, ...) extends ServiceModule with GlobalActorSystem
...
import play.api.Play.current lazy val app = new PlayServiceModule( play.api.libs.concurrent.Akka.system, ...)
Bridge the Play framework to an application cake
Thanks!