Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
OO in Scala - The unloved side of the cake?
Search
Tobias Neef
April 17, 2013
Programming
2
120
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
Tweet
Share
More Decks by Tobias Neef
See All by Tobias Neef
One night at a METRO depot
tobnee
0
84
Reactive Play and Scala
tobnee
1
270
Behavioural Abstractions in Play
tobnee
0
150
Funktionale Parallelität in Scala
tobnee
0
50
Functional Parallel Architecture
tobnee
1
92
Play Techtalk - Async Play
tobnee
2
500
Other Decks in Programming
See All in Programming
デザインパターンで理解するLLMエージェントの作り方 / How to develop an LLM agent using agentic design patterns
rkaga
9
1.7k
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
230
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
130
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
140
CSC509 Lecture 13
javiergs
PRO
0
110
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
260
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
270
RubyLSPのマルチバイト文字対応
notfounds
0
120
@nifty天気予報のフロントエンドを 実装するまで - NIFTY Tech Talk #22
niftycorp
PRO
0
100
Jakarta EE meets AI
ivargrimstad
0
820
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
3
260
Featured
See All Featured
We Have a Design System, Now What?
morganepeng
50
7.2k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
A Tale of Four Properties
chriscoyier
156
23k
Unsuck your backbone
ammeep
668
57k
Facilitating Awesome Meetings
lara
50
6.1k
4 Signs Your Business is Dying
shpigford
180
21k
Designing for humans not robots
tammielis
250
25k
GraphQLとの向き合い方2022年版
quramy
43
13k
Rails Girls Zürich Keynote
gr2m
94
13k
Building Adaptive Systems
keathley
38
2.3k
How to Ace a Technical Interview
jacobian
276
23k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
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!