我可以使用Dagger将不同的值注入到对象图深处的同一个类的多个实例中吗?我想避免通过图中的包含对象传递值(因此我可以更改包含对象的实现而不影响其容器).
这是一个人为的例子.对象图是一个Top,它包含一个Left和Right,每个都包含一个Show.所以Show有两个实例.
class Top {
Left left;
Right right;
void encodeTwice(String data) {
left.encode(data);
right.encode(data.getBytes());
}
}
class Left {
Leaf leaf;
void encode(String data) {
leaf.write(URLEncoder.encode(data));
}
}
class Right {
Leaf leaf;
void encode(byte[] data) {
leaf.write(DatatypeConverter.printBase64Binary(data));
}
}
interface Leaf {
void write(String data);
}
class Show implements Leaf {
String label;
@Override public void write(String data) {
System.out.println(label + ": " + data);
}
}
// There might be other classes that implement Leaf. …Run Code Online (Sandbox Code Playgroud)