论坛首页 Java企业应用论坛

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

浏览 5298 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (9)
作者 正文
   发表时间:2009-02-07   最后修改:2009-02-07
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);	
	}
}

 

   发表时间:2009-02-07  
物体发出------分发事件--------》自身---------发出处理事件------》监听器

第一步:物体可以安装不同种类的监听器,这样可以处理不同类型的消息。
通过组装的方式:
class ObjcetClass{
    ListenerInterface001 listener1;(在Swing中实际上是一个二叉树。)
    ListenerInterface002 listener2;
    ListenerInterface003 listener3;  
}
第二步:构造监听器,通过实现:
class Listener1 implements ListenerInterface001 {
}
class Listener2 implements ListenerInterface002{
}
class Listener3 implements ListenerInterface003{
}
第三步:将监听器安装到物体上(或者通过ADD接口):
listener1=new Listener1();(Swing 中是把,新的监听器安装到二叉树上)
listener2=new Listener2();
listener3=new Listener3();
第四步:
不同的事件映射为不同的监听器的实现,通过switch语句:
event1-->listener1
event2-->listener2
event3-->listener3  

注意:以上步骤中,监听器的实现优为重要。
因为监听器要访问发出事件的物体本身,因此,经常利用匿名类。
0 请登录后投票
   发表时间:2009-02-07  
物件是台湾术语吧
1 请登录后投票
   发表时间:2009-02-11  
Durian 写道
物件是台湾术语吧

物件就是对象吧
0 请登录后投票
   发表时间:2009-02-18  
二叉树的核心思想是什么?
1 请登录后投票
   发表时间:2009-02-18  
histan 写道
二叉树的核心思想是什么?

二叉树核心思想太多了:
    比如我的例子中,实际上是监听器放到叶子上,然后把叶子给遍历了一遍,却写了很少的代码:
class Tree{
       Tree left;//左边是树。
       Tree right;//右边是叶子。(在上面例子中)   
       getAccessToLeaf(){
          if(this.left!=null){
          this.left.getAccessToLeaf();//遍历左子树。
          }
          if(this.right!=null){
           this.right.getAccessToLeaf();//遍历右子树。
          }
          //如果左右都没有了,也就是叶子,打印以下语句。
          System.out.println("hello!")
       }
}
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics