流沙团
观察者设计模式
2016-8-7 流沙团
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;
}
}
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容