观察者模式

观察者模式要达到的目的:在此种模式中,一个目标物件管理所有相依于它的观察者物件,并且在它本身的状态改变时主动发出通知。这通常透过呼叫各观察者所提供的方法来实现。此种模式通常被用来实现事件处理系统。

1.类图


2.具体实例

A.txt


1
腾辉公司需要个java程序员。海景公司需要个动画设计师。仁海公司需要个电工。

Application


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package Observer;

/**
* Created by 15581 on 2017/11/8.
*/ // Observer.Application.java
public class Application{
public static void main(String args[]){
SeekJobCenter center=new SeekJobCenter();
UniverStudent zhangLin=new UniverStudent(center,"A.txt");
HaiGui wangHao=new HaiGui(center,"B.txt");
center.giveNewMess("腾辉公司需要个java程序员。");
center.notifyObservers();
center.giveNewMess("海景公司需要个动画设计师。");
center.notifyObservers();
center.giveNewMess("仁海公司需要个电工。");
center.notifyObservers();
center.giveNewMess("仁海公司需要个电工。");
center.notifyObservers();
}
}

B.txt


1
腾辉公司需要个java程序员。

HaiGui


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package Observer;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
* Created by 15581 on 2017/11/8.
*/
public class HaiGui implements Observer{
Subject subject;
File myFile;
HaiGui(Subject subject,String fileName){
this.subject=subject;
subject.addObserver(this);
myFile=new File(fileName);
}
public void hearTelephone(String heardMess){
try{ boolean boo=heardMess.contains("java程序员")||heardMess.contains("软件");
if(boo){
RandomAccessFile out=new RandomAccessFile(myFile,"rw");
out.seek(out.length());
byte [] b=heardMess.getBytes();
out.write(b);
System.out.print("我是一个海归,");
System.out.println("我向文件"+myFile.getName()+"写入如下内容:");
System.out.println(heardMess);
}
else{
System.out.println("我是海归,这次的信息中没有我需要的信息");
}
}
catch(IOException exp){
System.out.println(exp.toString());
}
}
}

Observer


1
2
3
4
5
6
package Observer;

// Observer.Observer.java
public interface Observer{
public void hearTelephone(String heardMess);
}

SeekJobCenter


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package Observer;

import java.util.ArrayList;

/**
* Created by 15581 on 2017/11/8.
*/
public class SeekJobCenter implements Subject{
String mess;
boolean changed;
ArrayList<Observer> personList;
SeekJobCenter(){
personList=new ArrayList<Observer>();
mess="";
changed=false;
}
public void addObserver(Observer o){
if(!(personList.contains(o)))
personList.add(o);
}
public void deleteObserver(Observer o){
if(personList.contains(o))
personList.remove(o);
}
public void notifyObservers(){
if(changed){
for(int i=0;i<personList.size();i++){
Observer observer=personList.get(i);
observer.hearTelephone(mess);
}
changed=false;
}
}
public void giveNewMess(String str){
if(str.equals(mess))
changed=false;
else{
mess=str;
changed=true;
}
}
}

Subject


1
2
3
4
5
6
7
8
9
10
package Observer;

/**
* Created by 15581 on 2017/11/8.
*/ // Observer.Subject.java
public interface Subject{
public void addObserver(Observer o);
public void deleteObserver(Observer o);
public void notifyObservers();
}

UniverStudent


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package Observer;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
* Created by 15581 on 2017/11/8.
*/
public class UniverStudent implements Observer{
Subject subject;
File myFile;
UniverStudent(Subject subject,String fileName){
this.subject=subject;
subject.addObserver(this); //使当前实例成为subject所引用的具体主题的观察者
myFile=new File(fileName);
}
public void hearTelephone(String heardMess){
try{ RandomAccessFile out=new RandomAccessFile(myFile,"rw");
out.seek(out.length());
byte [] b=heardMess.getBytes();
out.write(b); //更新文件中的内容
System.out.print("我是一个大学生,");
System.out.println("我向文件"+myFile.getName()+"写入如下内容:");
System.out.println(heardMess);
}
catch(IOException exp){
System.out.println(exp.toString());
}
}
}