Last active
August 29, 2015 14:21
-
-
Save ciscoheat/a9c68f00e55b649b0c67 to your computer and use it in GitHub Desktop.
Minimal working DCI example for Haxe
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
/* | |
How to run: | |
1. Download and install Haxe: http://haxe.org/download/ | |
2. Save this code to Main.hx and open a command prompt in the same directory | |
3. Execute "haxelib install haxedci" | |
4. Execute "haxe -main Main -lib haxedci --interp" | |
For javascript output: | |
haxe -main Main -lib haxedci -js moneytransfer.js | |
*/ | |
class Account { | |
public var name : String; | |
public var balance : Int; | |
public function new(name, balance) { | |
this.name = name; | |
this.balance = balance; | |
} | |
public function increaseBalance(amount: Int) { | |
balance += amount; | |
} | |
public function decreaseBalance(amount: Int) { | |
balance -= amount; | |
} | |
} | |
class MoneyTransfer implements haxedci.Context { | |
public function new(source, destination, amount) { | |
this.source = source; | |
this.destination = destination; | |
this.amount = amount; | |
} | |
public function transfer() { | |
source.withdraw(); | |
} | |
@role var source : { | |
function decreaseBalance(a : Int) : Void; | |
} = | |
{ | |
function withdraw() : Void { | |
self.decreaseBalance(amount); | |
destination.deposit(); | |
} | |
} | |
@role var destination : { | |
function increaseBalance(a : Int) : Void; | |
} = | |
{ | |
function deposit() : Void { | |
self.increaseBalance(amount); | |
} | |
} | |
@role var amount : Int; | |
} | |
class Main { | |
static function main() { | |
var savings = new Account("Savings", 1000); | |
var home = new Account("Home", 0); | |
trace("Before transfer:"); | |
trace(savings.name + ": $" + savings.balance); | |
trace(home.name + ": $" + home.balance); | |
// Creating and executing the Context: | |
new MoneyTransfer(savings, home, 500).transfer(); | |
trace("After transfer:"); | |
trace(savings.name + ": $" + savings.balance); | |
trace(home.name + ": $" + home.balance); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment