Your SlideShare is downloading. ×
Scala Cake Pattern                      The dependency non-injection un-frameworkúterý, 29. ledna 13
The cake is a lie                      • No injection                      • No framework                      • Just Scal...
The patternúterý, 29. ledna 13
The top level                      object DeluxeEspressoMachine                       extends EspressoMachine             ...
The top level                      object DeluxeEspressoMachine                       extends EspressoMachine             ...
The top level                      object DeluxeEspressoMachine                       extends EspressoMachine             ...
The Espresso Machine                       trait EspressoMachine {                        self: WaterHeaterComponent      ...
The Espresso Machine                       trait EspressoMachine {                         self: WaterHeaterComponent     ...
The Espresso Machine                       trait EspressoMachine {                         self: WaterHeaterComponent     ...
The self-typeúterý, 29. ledna 13
The Component                       trait GrinderComponent {                           def grinder: Grinder               ...
The Implementation                trait FineGrinderComponent                  extends GrinderComponent {                  ...
The top level                      object DeluxeEspressoMachine                       extends EspressoMachine             ...
Pro/Cons                      • Pros                       • Comes with the language                       • Statically ty...
The language features                      • Trait                      • Self-type                      • Singletonúterý,...
References                      • http://www.cakesolutions.net/teamblogs/                        2011/12/19/cake-pattern-i...
Upcoming SlideShare
Loading in...5
×

Dependency Injection in Scala - Cake Pattern

2,934

Published on

Second part of the presentation about dependency injection in Scala from January's meetup of the Czech Scala Enthusiasts.
This part addresses the Cake Pattern - Scala's direct language support for DI.

Published in: Technology

Transcript of "Dependency Injection in Scala - Cake Pattern"

  1. 1. Scala Cake Pattern The dependency non-injection un-frameworkúterý, 29. ledna 13
  2. 2. The cake is a lie • No injection • No framework • Just Scala language featuresúterý, 29. ledna 13
  3. 3. The patternúterý, 29. ledna 13
  4. 4. The top level object DeluxeEspressoMachine extends EspressoMachine with FineGrinderComponent with AutomaticFoamerComponent with FastWaterHeaterComponent object BudgetEspressoMachine extends EspressoMachine with RoughGrinderComponent with ManualFoamerComponent with SlowWaterHeaterComponentúterý, 29. ledna 13
  5. 5. The top level object DeluxeEspressoMachine extends EspressoMachine with FineGrinderComponent with AutomaticFoamerComponent with FastWaterHeaterComponentúterý, 29. ledna 13
  6. 6. The top level object DeluxeEspressoMachine extends EspressoMachine with FineGrinderComponent with AutomaticFoamerComponent with FastWaterHeaterComponentúterý, 29. ledna 13
  7. 7. The Espresso Machine trait EspressoMachine { self: WaterHeaterComponent with GrinderComponent with FoamerComponent => def makeCoffee = { val beans = grinder.grind val water = heater.heatWater val foam = foamer.makeFoam ... } }úterý, 29. ledna 13
  8. 8. The Espresso Machine trait EspressoMachine { self: WaterHeaterComponent with GrinderComponent with FoamerComponent => def makeCoffee = { val beans = grinder.grind val water = heater.heatWater val foam = foamer.makeFoam ... } }úterý, 29. ledna 13
  9. 9. The Espresso Machine trait EspressoMachine { self: WaterHeaterComponent with GrinderComponent with FoamerComponent => def makeCoffee = { val beans = grinder.grind val water = heater.heatWater val foam = foamer.makeFoam ... } }úterý, 29. ledna 13
  10. 10. The self-typeúterý, 29. ledna 13
  11. 11. The Component trait GrinderComponent { def grinder: Grinder trait Grinder { def grind: GroundBeans } }úterý, 29. ledna 13
  12. 12. The Implementation trait FineGrinderComponent extends GrinderComponent { val grinder = new FineGrinder class FineGrinder extends Grinder { def grind = GroundBeans(GrindFineness.Fine) } }úterý, 29. ledna 13
  13. 13. The top level object DeluxeEspressoMachine extends EspressoMachine with FineGrinderComponent with AutomaticFoamerComponent with FastWaterHeaterComponentúterý, 29. ledna 13
  14. 14. Pro/Cons • Pros • Comes with the language • Statically typed & immutable • Compiler helps a lot • Cons • No XML and annotationsúterý, 29. ledna 13
  15. 15. The language features • Trait • Self-type • Singletonúterý, 29. ledna 13
  16. 16. References • http://www.cakesolutions.net/teamblogs/ 2011/12/19/cake-pattern-in-depth/ • http://jonasboner.com/2008/10/06/real- world-scala-dependency-injection-di/ • http://www.warski.org/blog/2010/12/di-in- scala-cake-pattern/ • http://lamp.epfl.ch/~odersky/papers/ ScalableComponent.pdfúterý, 29. ledna 13

×