背景:
从最开始的开门小例子到后来的,使用C#事件与委托到java的事件与委托,老师针对发消息的人和收消息人的提出了监听器的概念,java的监听器是怎么实现的呢?是放置了两个空壳放置发消息的人和收消息的人。
过程
图:
讲完课以后
大概的图(这个还需要完善)
代码:
package 米老师讲的两个事件与委托2023年5月7日; public class Client { public static void main(String[] args) throws Exception { Notice notice = new Notice(); notice.notice(); } }
public class Notice { public void notice() throws Exception { Listener listener = new Listener(); listener.addRegister(new CatEvent(),new Cat("Tom"), "shout"); listener.addDelegate(new MouseEvent(),new Mouse("jerry"), "run"); listener.addDelegate(new MouseEvent(),new Mouse("jack"), "run"); listener.invoke(); } }
public class Register { protected Object obj; protected String methodName; protected Object[] methodParameter; protected Class<?>[] methodType; protected static List<Register> registerList=new ArrayList<>(); public Register(){} public Register(Object obj, String methodName, Object... methodParameter) { this.obj = obj; this.methodName = methodName; this.methodParameter = methodParameter; int len = methodParameter.length; this.methodType = new Class[len]; for (int i = 0; i < len; i++) { methodType[i] = methodParameter[i].getClass(); } } public void invoke() throws Exception { Method method = obj.getClass().getDeclaredMethod(methodName, methodType); method.invoke(obj, methodParameter); } public void addRegister(Register register){ } }``` ```bash public class CatEvent extends Register { public CatEvent() { super(); } @Override public void invoke() throws Exception { for (Register d : registerList) { d.invoke(); } } @Override public void addRegister(Register delegate) { System.out.println(delegate.toString()); registerList.add(delegate); } }
public class RegisterFather { }
public class Cat extends RegisterFather { private String name; public Cat(String name){ this.name = name; } public void shout() { System.out.println("我是"+name+",我来了"); } }
public class Cat extends RegisterFather { private String name; public Cat(String name){ this.name = name; } public void shout() { System.out.println("我是"+name+",我来了"); } }
public class Delegate { protected Object obj; protected String methodName; protected Object[] methodParameter; protected Class<?>[] methodType; protected static List<Delegate> delegateList=new ArrayList<>(); public Delegate(){} public Delegate(Object obj, String methodName, Object... methodParameter) { this.obj = obj; this.methodName = methodName; this.methodParameter = methodParameter; int len = methodParameter.length; this.methodType = new Class[len]; for (int i = 0; i < len; i++) { methodType[i] = methodParameter[i].getClass(); } } public void invoke() throws Exception { Method method = obj.getClass().getDeclaredMethod(methodName, methodType); method.invoke(obj, methodParameter); } public void addDelegate(Delegate delegate){ } }
public class MouseEvent extends Delegate{ public MouseEvent() { super(); } @Override public void invoke() throws Exception { for (Delegate d : delegateList) { d.invoke(); } } @Override public void addDelegate(Delegate delegate) { System.out.println(delegate.toString()); delegateList.add(delegate); } public void delDelegate(Delegate delegate) { delegateList.remove(delegate); } }
public class DelegateFather { }
public class Mouse extends DelegateFather { private String name; public Mouse(String name){ this.name = name; } public void run(){ System.out.println("我是"+name+",我跑了"); } }
2023年5月9日22:37:21
更加完善了一下图,抽象出来发消息和收消息的接口
2023年5月11日10:16:36
完善了一版抽象出来体现多态的接口,使用配置文件读取确定运行时传入什么
基于上一次
public class Notice { public void notice() throws Exception { // Listener listener = new Listener(); // MouseEvent mouseEvent = new MouseEvent(); // // listener.addRegister(new CatEvent(),new Cat("Tom"), "shout"); // listener.addDelegate(mouseEvent,new Mouse("jerry"), "run"); // listener.addDelegate(mouseEvent,new Mouse("jack"), "run"); // listener.invoke(); Listener listener = new Listener(); CatEvent catEvent = new CatEvent(); MouseEvent mouseEvent = new MouseEvent(); Properties properties = new Properties(); properties.load(new FileInputStream("E:\\zy\\TGB-zgy-2022\\米老师设计模式课相关资料必须留着\\米老师设计模式课小例子\\JAVAtest\\entrust\\src\\main\\resources\\config.properties")); String catPath = (String) properties.get("catPath"); String catMethod = (String) properties.get("catMethod"); String[] catList = properties.get("catList").toString().split(","); String mousePath = (String) properties.get("mousePath"); String mouseMethod = (String) properties.get("mouseMethod"); String[] mouseList = properties.get("mouseList").toString().split(","); for (String catName : catList) { Object catInstance = Class.forName(catPath).getConstructor(String.class).newInstance(catName); listener.addRegister(catEvent, (ISend) catInstance, catMethod); } for (String mouseName : mouseList) { Object mouseInstance = Class.forName(mousePath).getConstructor(String.class).newInstance(mouseName); listener.addDelegate(mouseEvent, (IReceive) mouseInstance, mouseMethod); } listener.invoke(); } }
配置文件
catPath =end20230507.Cat catMethod = send catList = Tom,Tom4 mousePath =end20230507.Mouse mouseMethod = receive mouseList = Jerry,Ja
总结:
1加到n上是需要有n的前提的,知道java的监听器是怎么来的,将发消息和收消息的人进行匹配是在监听器中进行的,知道监听器是怎么来的,知其然,知其所以然。