我有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

最佳答案

只是尝试用一个例子完整地解释。您的代码会发生什么(为获得更好的可读性而修改)如下:

第一个例子

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 s,每个代表创建的Rat实例,即 Rat(4cf777e8) Rat(5702b3b1) Rat @ 69ea3742
// 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)

简化版

我想出的用于测试的代码的简化版本:
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();
}

10-04 10:02