Skip to content

Instantly share code, notes, and snippets.

@jdonaldson
Created October 16, 2010 22:15
Show Gist options
  • Save jdonaldson/630335 to your computer and use it in GitHub Desktop.
Save jdonaldson/630335 to your computer and use it in GitHub Desktop.
unject example
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