观察者设计模式

2016-8-7 流沙 JAVA

package com.gyarmy.testPerson;

public class Person {
	
	public void eat()
	{
		Event e = new Event(this);
		pl.doEat(e);
		System.out.println("吃......");
	}
	public void run()
	{
		Event e = new Event(this);
		pl.doRun(e);
		System.out.println("跑......");
	}
	
	private PersonListener pl;
	public void addPersonListener(PersonListener pl)
	{
		this.pl = pl;
	}
}

//事件监听器接口 
interface PersonListener{
	public  void doEat(Event e);
	public  void doRun(Event e);
	
}

//事件对象 --- 封装 事件源 
class Event{
	private Person p;
	public Event(Person p){
		
		this.p = p;
	}
	
	public Person getP()
	{
		return p;
	}
}

发表评论:

Powered by 流沙团