我有3个课程:
事件
public class Event<T> {
private List<BiConsumer<Object, T>> consumers = new ArrayList<>();
public void subscribe(BiConsumer<Object, T> consumer) {
consumers.add(consumer);
}
public void invoke(Object sender, T arg) {
for (BiConsumer<Object, T> consumer : consumers) {
System.out.println(sender + " " + "sender");
consumer.accept(sender, arg);
}
}
}
比率:
public class Rat {
private Game game;
public Rat(Game game) {
this.game = game;
game.ratEnters.subscribe((sender, arg) -> {
System.out.println( this + " " + sender);
});
System.out.println(this + "this");
game.ratEnters.invoke(this, null);
}
}
游戏:
public class Game {
public Event ratEnters = new Event();
}
和单项测试:
public class ObserverPatternTest {
@Test
public void singleRatTest() {
Game game = new Game();
Rat rat = new Rat(game);
Rat rat2 = new Rat(game);
Rat rat3 = new Rat(game);
}
}
输出:
design.patterns.behavioral.patterns.observer.exercises.Rat@4e515669this
design.patterns.behavioral.patterns.observer.exercises.Rat@4e515669 sender
design.patterns.behavioral.patterns.observer.exercises.Rat@4e515669 design.patterns.behavioral.patterns.observer.exercises.Rat@4e515669
design.patterns.behavioral.patterns.observer.exercises.Rat@17d10166this
design.patterns.behavioral.patterns.observer.exercises.Rat@17d10166 sender
design.patterns.behavioral.patterns.observer.exercises.Rat@4e515669 design.patterns.behavioral.patterns.observer.exercises.Rat@17d10166
design.patterns.behavioral.patterns.observer.exercises.Rat@17d10166 sender
design.patterns.behavioral.patterns.observer.exercises.Rat@17d10166 design.patterns.behavioral.patterns.observer.exercises.Rat@17d10166
我的问题是:该实例与发送者实例不一样的可能性如何?
design.patterns.behavioral.patterns.observer.exercises.Rat@4e515669 design.patterns.behavioral.patterns.observer.exercises.Rat@17d10166
答案 0 :(得分:1)
仅尝试使用示例进行完整解释。您的代码会发生什么情况(为了获得更清晰的输出而进行了修改):
Rat rat1 = new Rat(game);
执行为:
this ----> pack.Sample$Rat@4cf777e8
sender ----> pack.Sample$Rat@4cf777e8
pack.Sample$Rat@4cf777e8 this ----> sender pack.Sample$Rat@4cf777e8
简单,对吧?我相信这是不言自明的,因为创建了一个实例( 4cf777e8 ),仅包含一个消费者的列表,一切正常。
然后在第二次初始化中
Rat rat2 = new Rat(game);
您现在已经创建了Rat
( 5702b3b1 )的另一个实例,现在在您的sender
通话中变成了Event.invoke
为:
// part-1
this ----> pack.Sample$Rat@5702b3b1
// part-2
sender ----> pack.Sample$Rat@5702b3b1
pack.Sample$Rat@4cf777e8 this ----> sender pack.Sample$Rat@5702b3b1
sender ----> pack.Sample$Rat@5702b3b1
pack.Sample$Rat@5702b3b1 this ----> sender pack.Sample$Rat@5702b3b1
您看到的输出分为两部分,第一部分显示当前的实例和发送者,这是创建的最新实例。
第二部分至关重要,因为它现在拥有List<BiConsumer<Object, T>>
,此列表具有先前的Rat
实例( 4cf777e8 )同样,这就是执行consumer.accept(sender, arg);
时第一个实例进入图片的原因,其中this
指向其实例,sender
指向当前的sender
已经成为争论的焦点。
进一步的初始化
Rat rat3 = new Rat(game);
您现在可以看到consumers
列表的增长方式及其对输出的影响。请记住,列表现在有BiConsumer
个,每个Rat
都创建一个// part-1
this ----> pack.Sample$Rat@69ea3742
// part-2
sender ----> pack.Sample$Rat@69ea3742
pack.Sample$Rat@4cf777e8 this ----> sender pack.Sample$Rat@69ea3742 // (first element in list)
sender ----> pack.Sample$Rat@69ea3742
pack.Sample$Rat@5702b3b1 this ----> sender pack.Sample$Rat@69ea3742 // (second element in list)
sender ----> pack.Sample$Rat@69ea3742
pack.Sample$Rat@69ea3742 this ----> sender pack.Sample$Rat@69ea3742 // (third element in list)
实例,即 Rat(4cf777e8) , < em> Rat(5702b3b1) 和 Rat @ 69ea3742 ,就像您在输出中看到的那样。
public static class Event {
private List<Consumer<Object>> consumers = new ArrayList<>();
void subscribe(Consumer<Object> consumer) {
consumers.add(consumer);
}
void invoke(Object sender) {
consumers.forEach(consumer -> {
System.out.println("sender ----> " + sender);
consumer.accept(sender);
});
}
}
public static class Rat {
private Game game;
Rat(Game game) {
this.game = game;
System.out.println("this ----> " + this);
game.ratEnters.subscribe((sender) -> System.out.println(this + " this ----> sender " + sender));
game.ratEnters.invoke(this);
}
}
public static class Game {
Event ratEnters = new Event();
}
您要测试的简化代码:
choc_type