Created
September 28, 2012 22:13
-
-
Save akshaal/3802339 to your computer and use it in GitHub Desktop.
DCI MoneyTransfer in Scala
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
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