observer design patten

Observer Design Patten
Let’s design a system to see how observer design patten works.
First we need a object to be watched , here it is one people.
The people class look like:
package test;

import java.io.File;
import java.net.URL;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class People {
private PeopleListener pl;
People(){
init();
}
void happy(){
System.out.println("i am happy!");
if(pl!=null){
pl.happy();
}
}
void cry(){

System.out.println("i am crying!");
if(pl!=null){
pl.cry();
}
}
void fight(){

System.out.println("i am fighting!");
if(pl!=null){
pl.fight();
}
}
public void setPl(PeopleListener pl) {
this.pl = pl;
}

private void init() {
Document document = null;
SAXReader reader = new SAXReader();
URL url = People.class.getClassLoader().getResource("listenerConfig.xml");
String realPath = url.getPath() ;
System.out.println("realpath is " + realPath);
try {
document = reader.read(realPath);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Node node = document.selectSingleNode("//peoplelister");
if(node == null) return;
String className = node.getText();
try {
this.pl = (PeopleListener) Class.forName(className).newInstance();
} catch (InstantiationException | IllegalAccessException
| ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
And we need a Listener interface , who difines which methods we care.
package test;

public interface PeopleListener {
void happy();
void cry();
void fight();
}

And we make two classes implements the listener interface one is good watcher , and one is bad watcher.
package test;

public class BadPeopleListener implements PeopleListener {

@Override
public void happy() {
System.out.println("do be happy!");

}

@Override
public void cry() {
System.out.println("do be cry!");

}

@Override
public void fight() {
System.out.println("don't fight!");

}

}

package test;

public class GoodPeopleListener implements PeopleListener {

@Override
public void happy() {
System.out.println(" you should be happy!");
}

@Override
public void cry() {
System.out.println("you should cry!");
}

@Override
public void fight() {
System.out.println("you should fight!");
}

}

And also a xml file to configue which observer we will use . when people init , it will read the config file and decide which observer we will use.
<?xml version="1.0" encoding="UTF-8"?>
<lister>
<peoplelister>test.BadPeopleListener</peoplelister>
</lister>
At last , we create a class with main method to test this system.
package test;

public class MainTest {

public static void main(String[] args){
// MyPeopleListener mypl = new MyPeopleListener();
People p = new People();
// p.setPl(mypl);
p.cry();
p.fight();
p.happy();
}
}


Many listener system is similar with this system , like ServletContextAttributeListener, ServletContextListener,HttpSessionListener, HttpSessionBindingListener, HttpSessionAttributeListener, HttpSessionActivationListener, ServletRequestListener,ServletRequestAttributeListener
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值