Skip to content

Instantly share code, notes, and snippets.

@akshaal
Created September 28, 2012 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akshaal/3802339 to your computer and use it in GitHub Desktop.
Save akshaal/3802339 to your computer and use it in GitHub Desktop.
DCI MoneyTransfer in Scala
case class Account(var amount: Int)
import MoneyTransfer._
class MoneyTransfer private (src: Account with SourceRole,
dst: Account with DestinationRole,
amount: Int) {
def transfer(): Unit = {
src transfer amount
}
private implicit class Source(acc: Account with SourceRole) {
def withdraw(amount: Int): Unit = {
acc.amount -= amount
}
def transfer(amount: Int): Unit = {
dst deposit amount
src withdraw amount
}
}
private implicit class Destination(acc: Account with DestinationRole) {
def deposit(amount: Int): Unit = {
acc.amount += amount
}
}
}
object MoneyTransfer {
trait SourceRole
trait DestinationRole
def apply(src: Account, dst: Account, amount: Int) =
new MoneyTransfer (src.asInstanceOf[Account with SourceRole],
dst.asInstanceOf[Account with DestinationRole],
amount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment