通知 网站从因情语写改为晴雨,这个网站的模板也从calmlog_ex改为 whimurmur

行为型模式-观察者模式

4697人浏览 / 0人评论 / | 作者:因情语写  | 分类: 设计模式与算法  | 标签: 设计模式与算法  | 

作者:因情语写

链接:https://www.proprogrammar.com/article/452

声明:请尊重原作者的劳动,如需转载请注明出处


  行为型模式又可以分成以下四类:第一类:通过父类与子类的关系进行实现。第二类:两个类之间。第三类:类的状态。第四类:通过中间类

  观察者模式属于第二类,两个类之间的关系

  观察者订阅主题,当主题更新时通知观察者

package behavior.pattern.betweenclass.observer;

public interface Observer {
    public void update();
}
package behavior.pattern.betweenclass.observer;

public class FirstKindObserver implements Observer {
    private int id;

    public FirstKindObserver(int id) {
        super();
        this.id = id;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        FirstKindObserver other = (FirstKindObserver) obj;
        if (id != other.id)
            return false;
        return true;
    }

    @Override
    public void update() {
        System.out.println("FirstKindObserver " + id + " has received!");
    }

}
package behavior.pattern.betweenclass.observer;

public class SecondKindObserver implements Observer {

    private String id;
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        SecondKindObserver other = (SecondKindObserver) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }

    public SecondKindObserver(String id) {
        super();
        this.id = id;
    }

    @Override
    public void update() {
        System.out.println("SecondKindObserver " + id + " has received!");
    }

}
package behavior.pattern.betweenclass.observer;

public interface Subject {
    /*增加观察者*/  
    public void add(Observer observer);  
      
    /*删除观察者*/  
    public void del(Observer observer);  
      
    /*通知所有的观察者*/  
    public void notifyObservers();  
      
    /*自身的操作*/  
    public void operation();  
}
package behavior.pattern.betweenclass.observer;

import java.util.Enumeration;
import java.util.Vector;

public abstract class AbstractSubject implements Subject{
    private Vector<Observer> vector = new Vector<Observer>();
    
    @Override  
    public void add(Observer observer) {  
        vector.add(observer);  
    }  
  
    @Override  
    public void del(Observer observer) {  
        vector.remove(observer);  
    }  
  
    @Override  
    public void notifyObservers() {  
        Enumeration<Observer> enumo = vector.elements();
        while(enumo.hasMoreElements()){
            enumo.nextElement().update();
        }  
    }  
}
package behavior.pattern.betweenclass.observer;

public class MySubject extends AbstractSubject {

    @Override
    public void operation() {
        System.out.println("update self!");  
        notifyObservers();
    }

}

  测试代码

package behavior.pattern.betweenclass.observer;

public class Client {
    public static void main(String[] args) {
        Subject sub = new MySubject();  
        sub.add(new FirstKindObserver(123));  
        sub.add(new SecondKindObserver("111"));
        sub.add(new FirstKindObserver(456));
        sub.add(new SecondKindObserver("222"));
          
        sub.operation();  
        
        sub.del(new SecondKindObserver("222"));
        
        sub.operation();
    }
}

  运行结果


亲爱的读者:有时间可以点赞评论一下

点赞(0) 打赏

全部评论

还没有评论!
广告位-帮帮忙点下广告