Java Mixin & Service Locator

嗯,俺就是和静态类型干上了。这次实现了两个东西,Mixin和Service Locator。代码说话:
public interface Kisser {
	void kissYourKissable();
}

public interface Kissable {
	void kiss();
}

public class Boy implements Kissable {
	public void kiss() {
		System.out.println("kiss");
	}
}

public abstract class KisserMixin implements Reborn, Kisser {
	private Kissable kissable = _(Kissable.class);
	public void kissYourKissable() {
		kissable.kiss();
	}
}

public abstract class Girl implements Kisser {

}

public class ContainerDemo extends RebornContainer {
	public ContainerDemo() {
		reg(Boy.class);
		reg(KisserMixin.class);
	}
	public static void main(String[] args) {
		new ContainerDemo().demo();
	}
	private void demo() {
		Girl girl = _(Girl.class);
		girl.kissYourKissable();
	}
}

Mixin体现在Girl implements Kisser上。在_(Girl.class)的时候,容器会把注册的Mixin给织入。Service Locator体现在private Kissable kissable = _(Kissable.class);上。因为mixin必然要导致类是abstract的,所以放弃了new,采用_(class)的办法来获得对象实例。强侵入性的代价的收益是实现了Service Locator和Mixin。

你可能感兴趣的:(java)