监听者模式:(Swing监听器竟然是二叉树!!牛比!)

xiaoxiao2021-02-28  49

package asf; interface Listener { } abstract class Event { //public abstract void happen(); public abstract int getId(); } class RainEvent extends Event { private static final int id = 1; /*public void happen() { // TODO Auto-generated method stub System.out.println("raining"); }*/ public String toString(){ return "raining"; } public int getId() { // TODO Auto-generated method stub return id; } } class AlarmEvent extends Event { private static final int id = 3; /*public void happen() { // TODO Auto-generated method stub System.out.println("Alarming"); }*/ public String toString(){ return "Alarming"; } public int getId() { // TODO Auto-generated method stub return id; } } class BroadcastEvent extends Event { private static final int id = 2; public int getId() { // TODO Auto-generated method stub return id; } /*public void happen() { // TODO Auto-generated method stub System.out.println("Broadcasting"); }*/ public String toString(){ return "Broadcasting"; } } interface RainListener extends Listener { public void rain(Event e); } interface RadioListener extends Listener { public void broadcast(Event e); } interface ClockListener extends Listener { public void alarm(Event e); } class ListenerCaster implements RainListener, RadioListener, ClockListener { Listener l1; Listener l2; ListenerCaster(Listener l1, Listener l2) { this.l1 = l1; this.l2 = l2; } public void rain(Event e) { // TODO Auto-generated method stub if(l1!=null){ ((RainListener) l1).rain(e); } if(l2!=null){ ((RainListener) l2).rain(e); } } public void broadcast(Event e) { // TODO Auto-generated method stub\ if(l1!=null){ ((RadioListener) l1).broadcast(e);} if(l2!=null){ ((RadioListener) l2).broadcast(e);} } public void alarm(Event e) { // TODO Auto-generated method stub if(l1!=null){ ((ClockListener) l1).alarm(e);} if(l2!=null){ ((ClockListener) l2).alarm(e);} } } class TheNotifiedObject { RainListener rl; RadioListener ral; ClockListener cl; public void addRainListener(RainListener rl) { this.rl = new ListenerCaster(this.rl, rl); } public void addRadioListener(RadioListener ral) { this.ral = new ListenerCaster(this.ral, ral); } public void addClockListener(ClockListener cl) { this.cl = new ListenerCaster(this.cl, cl); } protected void processEvent(Event e) { switch (e.getId()) { case 1: if(ral!=null){ rl.rain(e); } break; case 2: if(ral!=null){ ral.broadcast(e);} break; case 3: if(cl!=null){ cl.alarm(e);} break; } } } public class TheNotifiedObjectA extends TheNotifiedObject { TheNotifiedObjectA() { addClockListener(new ClockListener() { public void alarm(Event e) { // TODO Auto-generated method stub System.out.println(e); } }); addRainListener(new RainListener() { public void rain(Event e) { // TODO Auto-generated method stub System.out.println(e); } }); addRadioListener(new RadioListener() { public void broadcast(Event e) { // TODO Auto-generated method stub System.out.println(e); } }); } public static void main(String[] args){ Event e1=new AlarmEvent(); Event e2=new RainEvent(); Event e3=new BroadcastEvent(); TheNotifiedObjectA k=new TheNotifiedObjectA(); k.processEvent(e1); k.processEvent(e2); k.processEvent(e3); } }

 

转载请注明原文地址: https://www.6miu.com/read-73116.html

最新回复(0)