设计模式01观察者模式(java)

先发代码,有空来写内容。

observer1

 import java.util.Observer;
import java.util.Observable; //学生类(Student)继承Observable后变得可以被观察
class Student extends Observable{
//属性
private String name;
private boolean isSleep = false;
private boolean isLate = false;
//构造函数
public Student(){}
public Student(String name){
this.name = name;
}
//睡觉行为
public void sleep(){
System.out.println(name + " is sleeping");
isSleep = true;
setChanged();
notifyObservers(); }
public String toString(){
return name;
}
}
//教师类(Teacher)实现Observer接口成为观察者
class Teacher implements Observer{
@Override
public void update(Observable o,Object arg){
System.out.println("teacher: "+o+",wakeup!wakeup!");
}
}
//班主任类(ClassHead)实现Observer接口成为观察者
class ClassHead implements Observer{
@Override
public void update(Observable o,Object arg){
System.out.println("classHead: "+o+",cleanTheRoomAfterSchool");
}
}
public class ObserverDemo{
public static void main(String[] arguments){
Student tom = new Student("Tom");
tom.addObserver(new ClassHead());
tom.addObserver(new Teacher()); System.out.println("有 "+tom.countObservers()+" 个观察者在观察Tom");
tom.sleep(); }
}

observer2

import java.util.Map;
import java.util.HashMap;
import java.util.Observer;
import java.util.Observable; //学生类(Student)继承Observable后变得可以被观察
class Student extends Observable{
//属性
private String name;
private boolean isSleep = false;
private boolean isLate = false;
//构造函数
public Student(){}
public Student(String name){
this.name = name;
}
//睡觉行为
public void sleep(){
System.out.println(name + " is sleeping");
isSleep = true;
setChanged();
notifyObservers("sleep"); }
//迟到行为
public void late(){
System.out.println(name + " was lated");
isLate = true;
setChanged();
notifyObservers("late");
}
public String toString(){
return name;
}
}
//教师类(Teacher)实现Observer接口成为观察者
class Teacher implements Observer{
//参数arg作为key,该参数触发的回应作为value存储在Map中
private Map<String,String> info = new HashMap<String,String>();
public Teacher(){
info.put("sleep",",wakeup!wakeup!");
info.put("late",",come in.");
}
//添加键值对
public boolean addSay(String k,String v){
return info.put(k,v)==null?false:true;
}
@Override
public void update(Observable o,Object arg){
String say = info.get(arg);
if(say == null){
return;
}
System.out.println("teacher: "+o+say);
}
}
//班主任类(ClassHead)实现Observer接口成为观察者
class ClassHead implements Observer{
private Map<String,String> info = new HashMap<String,String>();
public ClassHead(){
info.put("sleep",",goOutWashBaFace");
info.put("late",",cleanTheRoom!");
}
public boolean addSay(String k,String v){
return info.put(k,v)==null?false:true;
}
@Override
public void update(Observable o,Object arg){
String say = info.get(arg);
if(say == null){
return;
}
System.out.println("classHead: "+o+say);
}
}
public class ObserverDemo{
public static void main(String[] arguments){
Student tom = new Student("Tom");
tom.addObserver(new ClassHead());
tom.addObserver(new Teacher()); System.out.println("有 "+tom.countObservers()+" 个观察者在观察Tom");
tom.sleep();
tom.late();
}
}

Observer与Obvsevable实现

 import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
//定义observer接口
interface observer{
public void update(observable o,Object arg);
}
//定义observable类
class observable{
//isChanged记录实例是否改变
private boolean isChanged;
//obsers存储观察者集合
private List<observer> obsers;
//构造函数初始化实例
public observable(){
isChanged = false;
obsers = new ArrayList<observer>();
}
//设置是否发生变化为false
protected void clearChanged(){
isChanged = false;
}
//设置是否发生变化为true
public void setChanged(){
isChanged = true;
}
//返回对象是否发生改变
public boolean hasChanged(){
return isChanged;
}
//添加观察者
public void addObserver(observer o){
obsers.add(o);
}
//删除指定观察者
public void deleteObserver(observer o){
obsers.remove(o);
}
//清空所有观察者
public void deleteObservers(){
obsers.clear();
}
//返回观察者数量
public int countObservers(){
return obsers.size();
} //如果对象发生改变就通知所有观察者,然后将变化状态设置为false
public void notifyObservers(){
notifyObservers(new Object());
}
public void notifyObservers(Object arg){
if(!hasChanged())
return;
Iterator it = obsers.iterator();
while(it.hasNext()){
((observer)(it.next())).update(this,arg);
}
clearChanged();
}
}
上一篇:英特尔实感3D摄像头


下一篇:如何在 Mac App上刷新页面?苹果电脑的刷新