Created
September 21, 2022 12:32
-
-
Save george-hawkins/a9db64f05e14ea7d191bc4cf85dd64f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example | |
import java.time.{Duration, ZonedDateTime} | |
trait Customer | |
trait Priority | |
trait OvenState | |
trait Cooker { | |
def cook(customer: Customer, deliveryTime: ZonedDateTime, priority: Priority): Boolean | |
} | |
object CookieNames extends Enumeration { | |
type CookieName = Value | |
val GingerSnaps, ChocolateChip = Value | |
} | |
import CookieNames.CookieName | |
abstract class CookieCooker(overState: OvenState, cookingTime: Duration, name: CookieName) extends Cooker | |
class DelegatingCookieCooker(overState: OvenState, cookingTime: Duration, name: CookieName)(cookFn: (Customer, ZonedDateTime, Priority) => Boolean) extends CookieCooker(overState, cookingTime, name) { | |
def cook(customer: Customer, deliveryTime: ZonedDateTime, priority: Priority): Boolean = cookFn(customer, deliveryTime, priority) | |
} | |
object DslExample extends App { | |
def addCooker(cooker: Cooker): Unit = ??? | |
def addCooker(overState: OvenState, cookingTime: Duration, name: CookieName)(cookFn: (Customer, ZonedDateTime, Priority) => Boolean): Unit = addCooker(new DelegatingCookieCooker(overState, cookingTime, name)(cookFn)) | |
def getOvenState(): OvenState = ??? | |
val cookingTime = Duration.ofMinutes(10) | |
addCooker(new CookieCooker(getOvenState(), cookingTime, CookieNames.GingerSnaps) { | |
override def cook(customer: Customer, deliveryTime: ZonedDateTime, priority: Priority): Boolean = { | |
// Use `customer`, `deliveryTime` and `priority` to cook a cookie and return true if successful. | |
??? | |
} | |
}) | |
addCooker(getOvenState(), cookingTime, CookieNames.GingerSnaps) { (customer, deliveryTime, priority) => | |
true | |
} | |
val addCookersX = addCooker(getOvenState(), cookingTime, CookieNames.GingerSnaps)(_) | |
addCookersX { (customer, deliveryTime, priority) => | |
true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment