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
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
96
Reactive Play and Scala
tobnee
1
280
Behavioural Abstractions in Play
tobnee
0
160
Funktionale Parallelität in Scala
tobnee
0
71
Functional Parallel Architecture
tobnee
1
110
Play Techtalk - Async Play
tobnee
2
520
Other Decks in Programming
See All in Programming
進化を続けるGo toolsの現在地 / The Current State of Ever-Evolving Go Tools
hond0413
0
230
全PRの83%がAIレビューだけでマージできるようになった開発組織はその後どうなったか
athug
1
1.7k
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
700
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
230
【QA Test Talk Vol.8】AI-DLC による Whole Team Approach の加速
pkshadeck
PRO
0
190
AWS CDK を「作」ってみた 〜フルスクラッチで見えた CDK の裏側〜 / aws-cdk-from-scratch
gotok365
3
2.8k
源内ハンズオン概要編
hideg
0
190
SlackアプリとLambdaの 連携を構築した話
pawn_4_s
1
120
そこに3びきプロダクトがいるじゃろう——生成AI時代における“価値が届かない理由”の構造
kosuket
0
500
ここ半年くらいでAIに作らせたR用ツール
eitsupi
0
380
琵琶湖の水は止められてもNet--HTTPのリトライは止められない / You might be able to stop the water flow of Lake Biwa but you can't stop Net::HTTP retries
luccafort
PRO
0
740
React本体のコードリーディング
high_g_engineer
1
140
Featured
See All Featured
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
390
Being A Developer After 40
akosma
91
590k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
620
What does AI have to do with Human Rights?
axbom
PRO
1
2.3k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
330
Typedesign – Prime Four
hannesfritz
42
3.1k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
Practical Orchestrator
shlominoach
191
12k
Embracing the Ebb and Flow
colly
88
5.1k
Writing Fast Ruby
sferik
630
63k
Paper Plane (Part 1)
katiecoart
PRO
1
10k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
2
3.8k
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!