码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-10-18 01:39:34      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:

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

observer1

 1 import java.util.Observer;
 2 import java.util.Observable;
 3 
 4 //学生类(Student)继承Observable后变得可以被观察
 5 class Student extends Observable{
 6     //属性
 7     private String name;
 8     private boolean isSleep = false;
 9     private boolean isLate = false;
10     //构造函数
11     public Student(){}
12     public Student(String name){
13         this.name = name;
14     }
15     //睡觉行为
16     public void sleep(){
17         System.out.println(name + " is sleeping");
18         isSleep = true;
19         setChanged();
20         notifyObservers();
21         
22     }
23     public String toString(){
24         return name;
25     }
26 }
27 //教师类(Teacher)实现Observer接口成为观察者
28 class Teacher implements Observer{
29     @Override
30     public void update(Observable o,Object arg){
31             System.out.println("teacher: "+o+",wakeup!wakeup!");
32     }
33 }
34 //班主任类(ClassHead)实现Observer接口成为观察者
35 class ClassHead implements Observer{
36     @Override
37     public void update(Observable o,Object arg){
38             System.out.println("classHead: "+o+",cleanTheRoomAfterSchool");
39     }
40 }
41 public class ObserverDemo{
42     public static void main(String[] arguments){
43         Student tom = new Student("Tom");
44         tom.addObserver(new ClassHead());
45         tom.addObserver(new Teacher());
46 
47         System.out.println("有 "+tom.countObservers()+" 个观察者在观察Tom");
48         tom.sleep();
49         
50     }
51 }

 

 

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();
    }
}

 

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

标签:

原文地址:http://www.cnblogs.com/urusai/p/5971632.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!