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
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