基于Guava API实现异步通知和事件回调

2022-12-19 0 689

责任编辑农圣《程序语言就该这种学》

1如前所述Java API同时实现通告监督机制

当爸爸妈妈们在街道社区发问时,假如有增设选定使用者提问,则相关联的使用者就会接到电子邮件通告,这是观测者商业模式的一类应用领域情景。很多爸爸妈妈可能会想不到MQ、触发器堆栈等,只不过JDK这类就提供更多这种的API。他们用标识符来还原成这种两个应用领域情景,具体来说建立GPer类。

/*** JDK提供更多的一类观测者的同时实现形式,被观测者*/public class GPer extends Observable{ private String name =”GPer闭环”; private static GPer gper = null; private GPer(){} public static GPer getInstance(){ if(null == gper){ gper = new GPer();} return gper;} public String getName(){ return name;} public void publishQuestion(Question question){ System.out.println(question.getUserName()+ “在”+ this.name +”上递交了两个难题。”); setChanged(); notifyObservers(question);}}

接着建立难题Question类。

public class Question { private String userName; private String content; public String getUserName(){ return userName;} public void setUserName(String userName){ this.userName = userName;} public String getContent(){ return content;} public void setContent(String content){ this.content = content;}}

接著建立同学Teacher类。

public class Teacher implements Observer { private String name; public Teacher(String name){ this.name = name;} public void update(Observable o, Object arg){ GPer gper =(GPer)o; Question question =(Question)arg; System.out.println(“======================”); System.out.println(name +”同学,晚安!\n”+ “您接到了两个源自”+ gper.getName()+ “的发问,期望您答疑。难题文本如下表所示:\n”+ question.getContent()+ “\n”+ “发问者:”+ question.getUserName());}}

最后编写客户端测试标识符。

public static void main(String[] args){ GPer gper = GPer.getInstance(); Teacher tom = new Teacher(“Tom”); Teacher jerry = new Teacher(“Jerry”); gper.addObserver(tom); gper.addObserver(jerry);//使用者行为 Question question = new Question(); question.setUserName(“张三”); question.setContent(“观测者商业模式适用于哪些情景?”); gper.publishQuestion(question);}

运行结果如下表所示图所示。

基于Guava API实现异步通知和事件回调

2如前所述Guava API轻松落地观测者模式

笔者向大家推荐两个同时实现观测者商业模式的非常好用的框架,API使用也非常简单,举个例子,具体来说引入Maven依赖包。

com.google.guavaguava20.0

接着建立侦听该事件GuavaEvent。

/*** Created by Tom */public class GuavaEvent {@Subscribe public void subscribe(String str){//业务逻辑 System.out.println(“执行subscribe方法,传入的参数是:”+ str);}}

最后编写客户端测试标识符。

/*** Created by Tom */public class GuavaEventTest { public static void main(String[] args){ EventBus eventbus = new EventBus(); GuavaEvent guavaEvent = new GuavaEvent(); eventbus.register(guavaEvent); eventbus.post(“Tom”);}}

3使用观测者商业模式设计鼠标该事件响应API

再来设计两个业务情景,帮助爸爸妈妈们更好地理解观测者商业模式。在JDK源码中,观测者商业模式的应用领域也非常多。例如java.awt.Event是观测者商业模式的一类,只不过Java很少被用来写桌面程序。他们用标识符来同时实现一下,以帮助爸爸妈妈们更深刻地了解观测者商业模式的同时实现原理。具体来说,建立EventListener接口。

/*** 观测者抽象* Created by Tom.*/public interface EventListener {}

建立Event类。

/*** 标准该事件源格式的定义* Created by Tom.*/public class Event {//该事件源,动作是由谁发出的 private Object source;//该事件触发,要通告谁(观测者) private EventListener target;//观测者的回应 private Method callback;//该事件的名称 private String trigger;//该事件的触发该事件 private long time; public Event(EventListener target, Method callback){ this.target = target; this.callback = callback;} public Object getSource(){ return source;} public Event setSource(Object source){ this.source = source; return this;} public String getTrigger(){ return trigger;} public Event setTrigger(String trigger){ this.trigger = trigger; return this;} public long getTime(){ return time;} public Event setTime(long time){ this.time = time; return this;} public Method getCallback(){ return callback;} public EventListener getTarget(){ return target;} @Override public String toString(){ return “Event{“+ “source=”+ source +”, target=”+ target +”, callback=”+ callback +”, trigger=”+ trigger +\+ “, time=”+ time +};}}

建立EventContext类。

/*** 被观测者的抽象* Created by Tom.*/public abstract class EventContext { protected Map events = new HashMap(); public void addListener(String eventType, EventListener target, Method callback){ events.put(eventType,new Event(target,callback));} public void addListener(String eventType, EventListener target){ try { this.addListener(eventType, target, target.getClass().getMethod(“on”+toUpperFirstCase(eventType), Event.class));}catch (NoSuchMethodException e){ return;} } private String toUpperFirstCase(String eventType){ char [] chars = eventType.toCharArray(); chars[0]-=32; return String.valueOf(chars);} private void trigger(Event event){ event.setSource(this); event.setTime(System.currentTimeMillis()); try { if (event.getCallback()!= null){ //用反射调用反弹函数 event.getCallback().invoke(event.getTarget(), event);} }catch (Exception e){ e.printStackTrace();} } protected void trigger(String trigger){ if(!this.events.containsKey(trigger)){return;} trigger(this.events.get(trigger).setTrigger(trigger));}}

接着建立MouseEventType接口。

/*** Created by Tom.*/public interface MouseEventType {//单击 String ONCLICK =”click”;//双击 String ONDOUBLECLICK =”doubleClick”;//弹起 String ONUP =”up”;//按下 String ONDOWN =”down”;//移动 String ONMOVE =”move”;//滚动 String ONWHEEL =”wheel”;//悬停 String ONOVER =”over”;//失去焦点 String ONBLUR =”blur”;//获得焦点 String ONFOCUS =”focus”;}

建立Mouse类。

/*** 具体的被观测者* Created by Tom.*/public class Mouse extends EventContext { public void click(){ System.out.println(“调用单击方法”); this.trigger(MouseEventType.ONCLICK);} public void doubleClick(){ System.out.println(“调用双击方法”); this.trigger(MouseEventType.ONDOUBLECLICK);} public void up(){ System.out.println(“调用弹起方法”); this.trigger(MouseEventType.ONUP);} public void down(){ System.out.println(“调用按下方法”); this.trigger(MouseEventType.ONDOWN);} public void move(){ System.out.println(“调用移动方法”); this.trigger(MouseEventType.ONMOVE);} public void wheel(){ System.out.println(“调用滚动方法”); this.trigger(MouseEventType.ONWHEEL);} public void over(){ System.out.println(“调用悬停方法”); this.trigger(MouseEventType.ONOVER);} public void blur(){ System.out.println(“调用获得焦点方法”); this.trigger(MouseEventType.ONBLUR);} public void focus(){ System.out.println(“调用失去焦点方法”); this.trigger(MouseEventType.ONFOCUS);}}

建立反弹方法MouseEventLisenter类。

/*** 观测者* Created by Tom.*/public class MouseEventListener implements EventListener { public void onClick(Event e){ System.out.println(“===========触发鼠标单击该事件==========”+ “\n”+ e);} public void onDoubleClick(Event e){ System.out.println(“===========触发鼠标双击该事件==========”+ “\n”+ e);} public void onUp(Event e){ System.out.println(“===========触发鼠标弹起该事件==========”+ “\n”+ e);} public void onDown(Event e){ System.out.println(“===========触发鼠标按下该事件==========”+ “\n”+ e);} public void onMove(Event e){ System.out.println(“===========触发鼠标移动该事件==========”+ “\n”+ e);} public void onWheel(Event e){ System.out.println(“===========触发鼠标滚动该事件==========”+ “\n”+ e);} public void onOver(Event e){ System.out.println(“===========触发鼠标悬停该事件==========”+ “\n”+ e);} public void onBlur(Event e){ System.out.println(“===========触发鼠标失去焦点该事件==========”+ “\n”+ e);} public void onFocus(Event e){ System.out.println(“===========触发鼠标获得焦点该事件==========”+ “\n”+ e);}}

最后编写客户端测试标识符。

public static void main(String[] args){ EventListener listener = new MouseEventListener(); Mouse mouse = new Mouse(); mouse.addListener(MouseEventType.ONCLICK,listener); mouse.addListener(MouseEventType.ONMOVE,listener); mouse.click(); mouse.move();}

责任编辑为“Tom弹架构”原创,转载请注明出处。技术在于分享,我分享我快乐!假如责任编辑对您有帮助,欢迎

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务