Java中完美实现C#的EventHandler事件模式,代码优雅解耦合

Posted okfine

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java中完美实现C#的EventHandler事件模式,代码优雅解耦合相关的知识,希望对你有一定的参考价值。

hzEvents

A cSharp-like event model for java, decoupe code gracefully.
Java中完美实现C#的Event-EventHandler-EventArgs事件模式,代码优雅解耦合。

用法

Import cn.hz.Events.jar in your project first, or import source code.
首先项目中引入cn.hz.Events.jar包,或源码。

Subject

Class which supply events and fire events.
包含事件并触发事件的类。

public class Subject {
    //Simple event
    Event event1 = new Event();
    //Event with arguments
    Event2<String, Integer> eventWithArg = new Event2<>();
    //Sample of removeListener
    Event3<Subject, Integer, Integer> eventRemoved = new Event3<>();
    public void start() {
        // fire events at the appropriate time
        onEvent1(new EventArgs());
        onEvent2(new EventArgs2<>("Event2", 2));
        onEvent3(new EventArgs3<>(this, 3, 3));
    }
    protected void onEvent1(EventArgs e) {
        if (!event1.isEmpty()) {
            event1.fire(this, e);
        }
    }
    protected void onEvent2(EventArgs2<String, Integer> e) {
        if (!eventWithArg.isEmpty()) {
            eventWithArg.fire(this, e);
        }
    }
    protected void onEvent3(EventArgs3<Subject, Integer, Integer> e) {
        if (!eventRemoved.isEmpty()) {
            eventRemoved.fire(this, e);
        }
    }
}

Observer

Class which subscribe events.
事件订阅者(或观察者).

public class Observer {
    public void bindEvents(Subject subject, EventChain chain) {
        // bind event listeners
        subject.event1.addListener((o, e) -> {
            System.out.println("event1 fired.");
        });
        subject.eventWithArg.addListener((o, e) -> {
            System.out.println("eventWithArg fired, args: " + e.getData1() + "," + e.getData2());
        });
        chain.chainEvent.addListener((o, e) -> {
            System.out.println("chainEvent fired, args: " + e.getData1() + "," + e.getData2());
        });
        EventHandler3<Subject, Integer, Integer> handler3 = this::onEvent3;
        subject.eventRemoved.addListener(handler3);
        //remove event binding. message of event3 will not be shown
        subject.eventRemoved.removeListener(handler3); //Target must be exactly the same as addListener() !
    }
    private void onEvent3(Object o, EventArgs3<Subject, Integer, Integer> e) {
        System.out.println("event3 fired, args: " + e.getData1() + "," + e.getData2() + "," + e.getData3());
    }
}

详见源码(Source code):https://github.com/hzsrc/hzEv...

以上是关于Java中完美实现C#的EventHandler事件模式,代码优雅解耦合的主要内容,如果未能解决你的问题,请参考以下文章

event & EventHandler

event & EventHandler

C# - 试图找到剩余的 EventHandler 防止控制超出范围

编写高质量代码改善C#程序的157个建议——建议41:实现标准的事件模型

C#控制台基础 类型为EventHandler的事件的用法

C# 中的EventHandler实例详解-转