Created
October 16, 2010 22:15
-
-
Save jdonaldson/630335 to your computer and use it in GitHub Desktop.
unject example
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
import unject.StandardKernel; | |
import unject.UnjectModule; | |
import haxe.rtti.Infos; | |
class Demo{ | |
public static function main() | |
{ | |
var kernel = new StandardKernel([new TestModule()]); | |
var samurai = kernel.get(Samurai); | |
samurai.attack("the evildoers"); | |
} | |
} | |
class TestModule extends UnjectModule | |
{ | |
public override function load() | |
{ | |
bind(Katana).toSelf().withParameter("sharpness", 100); | |
bind(Samurai).toSelf(); | |
bind(IWeapon).to(Sword); | |
} | |
} | |
class Katana implements Infos | |
{ | |
public var sharpness : Int; | |
public function new(sharpness : Int) | |
{ | |
this.sharpness = sharpness; | |
} | |
} | |
class Samurai implements Infos | |
{ | |
var weapon : IWeapon; | |
public function new(weapon : IWeapon) | |
{ | |
this.weapon = weapon; | |
} | |
public function attack(target : String) | |
{ | |
return weapon.hit(target); | |
} | |
} | |
class Sword implements IWeapon, implements Infos | |
{ | |
public function new(); | |
public function hit(target : String) | |
{ | |
return "Chopped " + target + " in half."; | |
} | |
} | |
interface IWeapon | |
{ | |
public function hit(target:String):String; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment