嗯,俺就是和静态类型干上了。这次实现了两个东西,Mixin和Service Locator。代码说话:
[code]
public interface Kisser {
void kissYourKissable();
}
[/code]
[code]
public interface Kissable {
void kiss();
}
[/code]
[code]
public class Boy implements Kissable {
public void kiss() {
System.out.println("kiss");
}
}
[/code]
[code]
public abstract class KisserMixin implements Reborn, Kisser {
private Kissable kissable = _(Kissable.class);
public void kissYourKissable() {
kissable.kiss();
}
}
[/code]
[code]
public abstract class Girl implements Kisser {
}
[/code]
[code]
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();
}
}
[/code]
Mixin体现在Girl implements Kisser上。在_(Girl.class)的时候,容器会把注册的Mixin给织入。Service Locator体现在private Kissable kissable = _(Kissable.class);上。因为mixin必然要导致类是abstract的,所以放弃了new,采用_(class)的办法来获得对象实例。强侵入性的代价的收益是实现了Service Locator和Mixin。
[code]
public interface Kisser {
void kissYourKissable();
}
[/code]
[code]
public interface Kissable {
void kiss();
}
[/code]
[code]
public class Boy implements Kissable {
public void kiss() {
System.out.println("kiss");
}
}
[/code]
[code]
public abstract class KisserMixin implements Reborn, Kisser {
private Kissable kissable = _(Kissable.class);
public void kissYourKissable() {
kissable.kiss();
}
}
[/code]
[code]
public abstract class Girl implements Kisser {
}
[/code]
[code]
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();
}
}
[/code]
Mixin体现在Girl implements Kisser上。在_(Girl.class)的时候,容器会把注册的Mixin给织入。Service Locator体现在private Kissable kissable = _(Kissable.class);上。因为mixin必然要导致类是abstract的,所以放弃了new,采用_(class)的办法来获得对象实例。强侵入性的代价的收益是实现了Service Locator和Mixin。