`
sdkongkong
  • 浏览: 39345 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

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



分享到:
评论

相关推荐

    Observer HeadFirst design pattern

    Observer HeadFirst design pattern C++ version

    com.designpattern.state_observer.rar

    用state模式和observer模式一些模式QQ在线状态 com.designpattern.state_observer.rar

    美团Java 岗 154 道面试题.zip

    100.在 Java 中,什么叫观察者设计模式(observer design pattern)? 101.使用工厂模式最主要的好处是什么?在哪里使用? 102.举一个用 Java 实现的装饰模式(decorator design pattern)?它是作用于对象层次还是类...

    02-observer-pattern

    head-first-design-pattern—02-observer-pattern(观察者模式),融入了个人的见解,里面包含错误的实现和正确的标准实现,你可以对比学习,加深对模式的理解

    C++设计模式(Design Pattern)范例源代码

    23种设计模式(Design Pattern)的C++实现范例,包括下面列出的各种模式,代码包含较详细注释。另外附上“设计模式迷你手册.chm”供参考。 注:项目在 VS2008 下使用。 创建型: 抽象工厂模式(Abstract Factory) 生成...

    observer_JavaDesignPattern_

    观察者模式,实现了表示层和数据逻辑的分离。在观察目标和观察者之间建立一个抽象的耦合

    Observer2DesignPattern

    Observer2DesignPattern

    36种最新设计模式整理

    Design Pattern: Simple Factory 模式 Design Pattern: Abstract Factory 模式 Design Pattern: Builder 模式 Design Pattern: Factory Method 模式 Design Pattern: Prototype 模式 Design Pattern: Singleton...

    设计模式面试专题1

    1.请列举出在JDK中几个常用的设计模式 2.什么是设计模式 4.在 Java 中,什么叫观察者设计模式(observer design pattern) 5.

    Java.Design.Patterns.1537192353

    Java design patterns with the Simplest real world examples which are...OBSERVER PATTERN STRATEGY PATTERN COMMAND PATTERN VISITOR PATTERN STATE PATTERN ITERATOR PATTERN INTERPRETER PATTERN MEMENTO PATTERN

    Design.Patterns.Explained.Simply

    If you have ever bought any programming books, you might have noticed that there are two types of them: books that are too short to understand the topic ...Observer State Strategy Template Method Visitor

    design-pattern-java.pdf

    撤销功能的实现——备忘录模式(三) 撤销功能的实现——备忘录模式(四) 撤销功能的实现——备忘录模式(五) 观察者模式-Observer Pattern 对象间的联动——观察者模式(一) 对象间的联动——观察者模式(二) ...

    《Java Design Patterns》高清完整英文PDF版

    Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all....

    设计模式 design pattern

    5.7 OBSERVER(观察者)—对象行为型 模式 194 5.8 STATE(状态)—对象行为型模式 201 5.9 STRATEGY(策略)—对象行为型 模式 208 5.10 TEMPLATE METHOD(模板方法) —类行为型模式 214 5.11 VISITOR(访问者)—...

    Design Patterns Elements of Reusable Object-Oriented Software

    • What Is a Design Pattern? • Design Patterns in Smalltalk MVC • Describing Design Patterns • The Catalog of Design Patterns • Organizing the Catalog • How Design Patterns Solve Design ...

    Android代码-hello-design-pattern

    hello-design-pattern Hello world using all 23 kinds of GoF design patterns. ... SplitHelloWorldFactory abstractFactory = AbstractFactory.select(AbstractFactory.Type.DesignPattern); HelloWorld abstractF

    ObserverPatternTest

    Observer Pattern的Prototype实现,其中参照了部分CEGUI的实现细节 使用cmake进行编译

    Apress.Pro.Design.Patterns.in.Swift

    The Observer Pattern Chapter 23. The Memento Pattern Chapter 24. The Strategy Pattern Chapter 25. The Visitor Pattern Chapter 26. The Template Method Pattern Part 5 - The MVC Pattern Chapter 27. The...

Global site tag (gtag.js) - Google Analytics