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
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
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
Transcript
78 slides
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!
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
290
Behavioural Abstractions in Play
tobnee
0
160
Funktionale Parallelität in Scala
tobnee
0
75
Functional Parallel Architecture
tobnee
1
110
Play Techtalk - Async Play
tobnee
2
530
Other Decks in Programming
See All in Programming
Spec-Driven Development - Why Vaadin Is the Perfect Fit
simas
PRO
0
240
Distance field of Julia set
kinakomoti321
0
320
Coding with AI: How to trust the storyteller (AWS Community Day Adria, Ljubljana 2026)
slobodan
0
140
선언형 햅틱 라이브러리 진동을 소개합니다
l2hyunwoo
1
310
Optunaを作ったときにやりたかったこと、その後 (2026/10/16, Optuna Meetup #3)
iwiwi
4
1.5k
私なりのハーネスエンジニアリング ~独自の言語処理系の開発を通して~
seosoft
0
200
Next.js: The final boss of Web Performance
guaca
0
400
TS 7.0 で baseUrl が使えなくなった今、パスエイリアスをどうするか
bicstone
2
620
AmebaブログはKMPから卒業します
kmars0605
0
350
조합이폭발하는디자인시스템이걸눈으로도테스트하라고요
giljongyu
0
750
Snowflake Openflow Connector利用の勘所 / 20261024developersio2026tokyo-snowflake-openflow
kasacchiful
PRO
0
330
FactoryBotをやめればRSpecは速くなるのか?
yuta_onishi_97
0
1.7k
Featured
See All Featured
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
2
2.2k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
760
Designing Powerful Visuals for Engaging Learning
tmiket
1
650
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
940
New Earth Scene 8
popppiees
5
2.8k
GitHub's CSS Performance
jonrohan
1033
470k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
BBQ
matthewcrist
90
10k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
1
310
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
690
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
460
The World Runs on Bad Software
bkeepers
PRO
72
12k