设计模式之观察者模式

一、观察者模式概念

   观察者模式,顾名思义,就是观察一个对象的变化而做出变化,观察的对象称之为观察者,被观察的对象称之为主体。就比如我们在课堂上上课,当老师在看着我们的时候我们就认真听课,而当老师转过身去的时候,很多同学就会开始开小差。在这当中我们就是观察者,老师就是主体,当老师的行为发生改变时,我们的行为就会做相应的改变,这就是所谓的观察者模式。

二、代码示例             

interface Subject{
    void addListener(Observer observer);
    void removeListener(Observer observer);
    void trunArround();
    void faceStudents();
}

class Teacher implements Subject{
     
    private List<Observer> observers = new ArrayList<Observer>();
    
    public void addListener(Observer observer){
        observers.add(observer);
    }
    
    
    public void removeListener(Observer observer){
        observers.remove(observer);
    }
    
    public void trunArround(){
        for (Observer observer : observers) {
            observer.downAction();
        }
    }
    
    public void faceStudents(){
        for (Observer observer : observers) {
            observer.upAction();
        }
    }
}

abstract class Observer{
    
    abstract void upAction();
    abstract void downAction();
}

class Student extends Observer{

    public  Student(Subject subject) {
        subject.addListener(this);
    } 
    
    @Override
    void upAction() {
        System.out.println("我在认真学习");
    }

    @Override
    void downAction() {
        System.out.println("我在玩手机");
    }
    
}

public static void main(String[] args) throws Exception{
        
        Teacher teacher = new Teacher();
        
        Student firstStudent = new Student(teacher);
        Student second = new Student(teacher);
        
        
        teacher.trunArround();
        teacher.faceStudents();
}

运行代码会输出下面的结果

这就是当老师的行动有所改变时,所有的学生都可以感知到,从而做出相应的反应。

   上面的实现模式是当老师行动有所改变时,会通知到相关的学生,我做了什么动作,你们快做出相应的反应。这是不合乎情理,不可能老师说我要转身了,你们要玩手机的快玩手机吧。所以在这种场景里,学生作为观察者,需要时刻关注老师的行为,学生主动去获取老师的状态,查看其状态是否已改变,从而做出相应的动作。所以观察者模式还有一种实现,就是观察者能够主动去获取主体的状态。下面是其的实现代码:

interface Subject{
    void addListener(Observer observer);
    void removeListener(Observer observer);
    void trunArround();
    void faceStudents();
}

class Teacher implements Subject{
    
    private Integer status = 1;
     
    private List<Observer> observers = new ArrayList<Observer>();
    
    public Integer getStatus() {
        return status;
    }


    public void setStatus(Integer status) {
        this.status = status;
    }


    public void addListener(Observer observer){
        observers.add(observer);
    }
    
    
    public void removeListener(Observer observer){
        observers.remove(observer);
    }
    
    public void trunArround(){
        System.out.println("老师转身");
        this.status = 2;
    }
    
    public void faceStudents(){
        System.out.println("老师面对学生");
        this.status = 1;
    }
}

abstract class Observer{
    
    abstract void upAction();
    abstract void downAction();
}

class Student extends Observer{

    public  Student(Subject subject) {
        subject.addListener(this);
    } 
    
    public void watchTeacher(Subject subject){
        if(subject instanceof Teacher){
            if(((Teacher) subject).getStatus() == 1){
                this.upAction();
            }else if(((Teacher) subject).getStatus() == 2){
                this.downAction();
            }
        }
    }
    
    @Override
    void upAction() 
    {    
        System.out.println("我在认真学习");
    }

    @Override
    void downAction() {
        System.out.println("我在玩手机");
    }
    
}

public static void main(String[] args) throws Exception{
        
        Teacher teacher = new Teacher();
        
        Student student = new Student(teacher);
        
        teacher.trunArround();
        student.watchTeacher(teacher);
        teacher.faceStudents();
        student.watchTeacher(teacher);
}

 

posted @ 2017-03-02 20:27  守、护  阅读(227)  评论(0编辑  收藏  举报