SlideShare a Scribd company logo
Cake pattern
1
Alex Fomin
@AlexSane
2
DI IoC
3
DI != IoC
4
DI <: IoC
5
Cake Pattern <: DI
6
Example
7
class ShoppingRobot(listProvider: ShopListProvider, location: GeoLocation) {
def goShopping() = {
val list = listProvider.get
val shop = location.findNearestShop
shop.map(buy(_, list))
}
def buy(shop:Shop, list:List[Item]) = ???
}
class ShopListProvider {
def get:List[Item] = ???
}
class GeoLocation {
def findNearestShop:Maybe[Shop] = ???
}
8
Dependencies
object Application extends App {
val shoplistProvider = new ShopListProvider();
val location = new GeoLocation();
val robot = new ShoppingRobot(shopListProvider, location);
robot.goShopping
}
9
InjectionConstructor
Cake pattern
10
trait ShopListProviderComponent {
val shopListProvider: ShopListProvider
trait ShopListProvider {
def get: List[Item]
}
}
trait GeoLocationComponent {
val geoLocation: GeoLocation
trait GeoLocation {
def findNearestShop: Maybe[Shop]
}
}
11
trait ShoppingRobotComponent {
val robot: ShoppingRobot
trait ShoppingRobot {
def goShopping: Maybe[Purchase]
}
}
trait ShopListProviderComponentImpl extends ShopListProviderComponent {
val shopListProvider: ShopListProvider = new ShopListProviderImpl
class ShopListProviderImpl extends ShopListProvider {
override def get: List[Item] = ???
}
}
trait GeoLocationComponentImpl extends GeoLocationComponent {
val geoLocation: GeoLocation = new GeoLocationImpl
class GeoLocationImpl extends GeoLocation {
override def findNearestShop: Maybe[Shop] = ???
}
}
12
trait ShoppingRobotComponentImpl extends ShoppingRobotComponent {
this: ShoppingRobotComponentImpl
with ShopListProviderComponent
with GeoLocationComponent
=>
val robot: ShoppingRobot = new DefaultRobot
class DefaultRobot extends ShoppingRobot {
override def goShopping: Maybe[Purchase] = {
val list = listProvider.get
val shop = location.findNearestShop
shop.map(buy(_, list))
}
}
}
13
Dependency
object Registry
extends ShoppingRobotComponentImpl
with GeoLocationComponentImpl
with ShopListProviderComponentImpl
object Application extends App {
val robot = Registry.robot
robot.goShopping
}
14
Injection
object FailedRegistry
extends ShoppingRobotComponentImpl
with GeoLocationComponentImpl
error: illegal inheritance;
self-type FailedRegistry.type does not conform to ShoppingRobotComponentImpl's selftype
ShoppingRobotComponentImpl with ShopListProviderComponent with GeoLocationComponent
object FailedRegistry extends ShoppingRobotComponentImpl with
GeoLocationComponentImpl
15
object TestRegistry
extends ShoppingRobotComponentImpl
with FakeGeoLocationComponentImpl
with FakeShopListProviderComponentImpl
class Registry2(shop: Shop)
extends ShoppingRobotComponentImpl
with ShopListProviderComponentImpl
with GeoLocationComponent {
val geoLocation: GeoLocation = new GeoLocation {
override def findNearestShop: Maybe[Shop] = Some(shop)
}
}
16
Pros and Cons
+ Pure Scala
+ Compile time
validation
17
− Boilerplate
overhead
− Weak lifecycle
control
Q&A
18

More Related Content

KEY
My Development Story
PDF
[scala.by] Launching new application fast
PPTX
AVU Conference 2013 Modes of Edu delivery final 0 1
PPTX
Power point
PPT
Visual and oral presentation mode
PPT
Tooled Composite Design Pattern presentation
PPTX
Delivery modes
PPTX
Innovations in Market Mix Modelling
My Development Story
[scala.by] Launching new application fast
AVU Conference 2013 Modes of Edu delivery final 0 1
Power point
Visual and oral presentation mode
Tooled Composite Design Pattern presentation
Delivery modes
Innovations in Market Mix Modelling

Viewers also liked (12)

PPT
Consumption Pattern and Expenditure
PPTX
Bakery products
PDF
Работа с Akka Сluster, @afiskon, scalaby#14
PPTX
Marketing Research Design
PDF
Effect of Marketing Mix Elements
PPTX
Presentation Of Research Work
PPT
Research Presentation Format
PPT
Research project ppt
PPTX
Marketing mix the 7 p's of marketing
PPTX
Basic Baking Ingredients and Their Functions
PPSX
Experimental research design
PDF
Cloud computing project report
Consumption Pattern and Expenditure
Bakery products
Работа с Akka Сluster, @afiskon, scalaby#14
Marketing Research Design
Effect of Marketing Mix Elements
Presentation Of Research Work
Research Presentation Format
Research project ppt
Marketing mix the 7 p's of marketing
Basic Baking Ingredients and Their Functions
Experimental research design
Cloud computing project report
Ad

Similar to Cake pattern. Presentation by Alex Famin at scalaby#14 (20)

PDF
Dsug 05 02-15 - ScalDI - lightweight DI in Scala
PDF
Structure your Play application with the cake pattern (and test it)
PDF
Effective Scala: Programming Patterns
ODP
Dependency injection in Scala
PDF
Mieux programmer grâce aux design patterns
PDF
Design Patterns in the 21st Century - Samir Talwar
PPTX
Di &amp; dagger
PPTX
Dependency injection with Scala
PDF
Design patterns in the 21st Century
PDF
Alex Theedom Java ee revisits design patterns
PDF
SE2016 Java Alex Theedom "Java EE revisits design patterns"
PDF
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
PDF
Dependency injection in scala
PPTX
gffhfghfgchfygnghS09-Design-Patterns.pptx
PDF
Enterprise Cake
PDF
The Basic Concept Of IOC
PDF
PukaPuka Presentation
PDF
Reviewing OOP Design patterns
PPTX
Responsible DI: Ditch the Frameworks
PDF
Parsley & Flex
Dsug 05 02-15 - ScalDI - lightweight DI in Scala
Structure your Play application with the cake pattern (and test it)
Effective Scala: Programming Patterns
Dependency injection in Scala
Mieux programmer grâce aux design patterns
Design Patterns in the 21st Century - Samir Talwar
Di &amp; dagger
Dependency injection with Scala
Design patterns in the 21st Century
Alex Theedom Java ee revisits design patterns
SE2016 Java Alex Theedom "Java EE revisits design patterns"
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
Dependency injection in scala
gffhfghfgchfygnghS09-Design-Patterns.pptx
Enterprise Cake
The Basic Concept Of IOC
PukaPuka Presentation
Reviewing OOP Design patterns
Responsible DI: Ditch the Frameworks
Parsley & Flex
Ad

More from Vasil Remeniuk (20)

PPTX
Product Minsk - РТБ и Программатик
PDF
Scala laboratory: Globus. iteration #3
PPTX
Testing in Scala by Adform research
PPTX
Spark Intro by Adform Research
PPTX
Types by Adform Research, Saulius Valatka
PPTX
Types by Adform Research
PPTX
Scalding by Adform Research, Alex Gryzlov
PPTX
Scalding by Adform Research, Alex Gryzlov
PPTX
Spark by Adform Research, Paulius
PPTX
Scala Style by Adform Research (Saulius Valatka)
PPTX
Spark intro by Adform Research
PPTX
SBT by Aform Research, Saulius Valatka
PDF
Scala laboratory: Globus. iteration #2
PPTX
Testing in Scala. Adform Research
PDF
Scala laboratory. Globus. iteration #1
PDF
Cassandra + Spark + Elk
PDF
Опыт использования Spark, Основано на реальных событиях
PDF
ETL со Spark
PDF
Funtional Reactive Programming with Examples in Scala + GWT
PDF
Vaadin+Scala
Product Minsk - РТБ и Программатик
Scala laboratory: Globus. iteration #3
Testing in Scala by Adform research
Spark Intro by Adform Research
Types by Adform Research, Saulius Valatka
Types by Adform Research
Scalding by Adform Research, Alex Gryzlov
Scalding by Adform Research, Alex Gryzlov
Spark by Adform Research, Paulius
Scala Style by Adform Research (Saulius Valatka)
Spark intro by Adform Research
SBT by Aform Research, Saulius Valatka
Scala laboratory: Globus. iteration #2
Testing in Scala. Adform Research
Scala laboratory. Globus. iteration #1
Cassandra + Spark + Elk
Опыт использования Spark, Основано на реальных событиях
ETL со Spark
Funtional Reactive Programming with Examples in Scala + GWT
Vaadin+Scala

Recently uploaded (20)

PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Cloud computing and distributed systems.
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Modernizing your data center with Dell and AMD
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
“AI and Expert System Decision Support & Business Intelligence Systems”
Digital-Transformation-Roadmap-for-Companies.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Big Data Technologies - Introduction.pptx
Machine learning based COVID-19 study performance prediction
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Diabetes mellitus diagnosis method based random forest with bat algorithm
NewMind AI Monthly Chronicles - July 2025
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Cloud computing and distributed systems.
The AUB Centre for AI in Media Proposal.docx
Chapter 3 Spatial Domain Image Processing.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
NewMind AI Weekly Chronicles - August'25 Week I
Modernizing your data center with Dell and AMD
The Rise and Fall of 3GPP – Time for a Sabbatical?

Cake pattern. Presentation by Alex Famin at scalaby#14

  • 2. 2
  • 8. class ShoppingRobot(listProvider: ShopListProvider, location: GeoLocation) { def goShopping() = { val list = listProvider.get val shop = location.findNearestShop shop.map(buy(_, list)) } def buy(shop:Shop, list:List[Item]) = ??? } class ShopListProvider { def get:List[Item] = ??? } class GeoLocation { def findNearestShop:Maybe[Shop] = ??? } 8 Dependencies
  • 9. object Application extends App { val shoplistProvider = new ShopListProvider(); val location = new GeoLocation(); val robot = new ShoppingRobot(shopListProvider, location); robot.goShopping } 9 InjectionConstructor
  • 11. trait ShopListProviderComponent { val shopListProvider: ShopListProvider trait ShopListProvider { def get: List[Item] } } trait GeoLocationComponent { val geoLocation: GeoLocation trait GeoLocation { def findNearestShop: Maybe[Shop] } } 11 trait ShoppingRobotComponent { val robot: ShoppingRobot trait ShoppingRobot { def goShopping: Maybe[Purchase] } }
  • 12. trait ShopListProviderComponentImpl extends ShopListProviderComponent { val shopListProvider: ShopListProvider = new ShopListProviderImpl class ShopListProviderImpl extends ShopListProvider { override def get: List[Item] = ??? } } trait GeoLocationComponentImpl extends GeoLocationComponent { val geoLocation: GeoLocation = new GeoLocationImpl class GeoLocationImpl extends GeoLocation { override def findNearestShop: Maybe[Shop] = ??? } } 12
  • 13. trait ShoppingRobotComponentImpl extends ShoppingRobotComponent { this: ShoppingRobotComponentImpl with ShopListProviderComponent with GeoLocationComponent => val robot: ShoppingRobot = new DefaultRobot class DefaultRobot extends ShoppingRobot { override def goShopping: Maybe[Purchase] = { val list = listProvider.get val shop = location.findNearestShop shop.map(buy(_, list)) } } } 13 Dependency
  • 14. object Registry extends ShoppingRobotComponentImpl with GeoLocationComponentImpl with ShopListProviderComponentImpl object Application extends App { val robot = Registry.robot robot.goShopping } 14 Injection
  • 15. object FailedRegistry extends ShoppingRobotComponentImpl with GeoLocationComponentImpl error: illegal inheritance; self-type FailedRegistry.type does not conform to ShoppingRobotComponentImpl's selftype ShoppingRobotComponentImpl with ShopListProviderComponent with GeoLocationComponent object FailedRegistry extends ShoppingRobotComponentImpl with GeoLocationComponentImpl 15
  • 16. object TestRegistry extends ShoppingRobotComponentImpl with FakeGeoLocationComponentImpl with FakeShopListProviderComponentImpl class Registry2(shop: Shop) extends ShoppingRobotComponentImpl with ShopListProviderComponentImpl with GeoLocationComponent { val geoLocation: GeoLocation = new GeoLocation { override def findNearestShop: Maybe[Shop] = Some(shop) } } 16
  • 17. Pros and Cons + Pure Scala + Compile time validation 17 − Boilerplate overhead − Weak lifecycle control