码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式11---观察者模式

时间:2014-12-11 18:51:35      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   ar   color   os   sp   for   

bubuko.com,布布扣

观察者模式实质就是提取:提取监听对象的过程是相同的,也就是说observer类为根据不同的subject的状态做出相应的反应。

package com.jayfulmath.designpattern.observer;

public abstract class Observer {
    
    protected String name;
    protected ISubject sub;    
    
    public Observer(String name, ISubject sub) {
        this.name = name;
        this.sub = sub;
    }
    public abstract void Update();
}
public interface ISubject {
    public void Attach(Observer obs);
    public void Detach(Observer obs);
    public void Broadcast();
    public String getSubjectStatus();
    public void setSubjectStatus(String subjectStatus);
}

public class NBALooker extends Observer {

    public NBALooker(String name, ISubject sub) {
        super(name, sub);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void Update() {
        System.out.println(sub.getSubjectStatus()+"\t"+name+"\tstop NBA Look");
    }

}

public class NBALooker extends Observer {

    public NBALooker(String name, ISubject sub) {
        super(name, sub);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void Update() {
        System.out.println(sub.getSubjectStatus()+"\t"+name+"\tstop NBA Look");
    }

}
public class StockLooker extends Observer {

    public StockLooker(String name, ISubject sub) {
        super(name, sub);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void Update() {
        // TODO Auto-generated method stub
        System.out.println(sub.getSubjectStatus()+"\t"+name+"\tstop Stock Look");
    }

}
import java.util.ArrayList;
import java.util.List;

public class Boss implements ISubject {
    
    List<Observer> _mObservers = new ArrayList<Observer>();    
    public String subjectStatus;
    
    public void Attach(Observer obs)
    {
        _mObservers.add(obs);
    }
    
    public void Detach(Observer obs)
    {
        _mObservers.remove(obs);
    }
    
    public void Broadcast()
    {
        //here may be asynchronous to notify observer
        for(Observer observer :_mObservers)
        {
            observer.Update();
        }
    }
    
    /**
     * @return the subjectStatus
     */
    public String getSubjectStatus() {
        return subjectStatus;
    }

    /**
     * @param subjectStatus the subjectStatus to set
     */
    public void setSubjectStatus(String subjectStatus) {
        this.subjectStatus = subjectStatus;
    }
}

main:

public class ObserverMain extends BasicExample {

    @Override
    public void startDemo() {
        ISubject huhanshang = new Boss();
        Observer maweidong = ObserverFactory.Factory("NBALooker", "maweidong", huhanshang);
        Observer zhangdashang = ObserverFactory.Factory("StockLooker", "zhangdashang", huhanshang);
        
        huhanshang.Attach(maweidong);
        huhanshang.Attach(zhangdashang);
        
        huhanshang.setSubjectStatus("I am back! Huhan Shang!");
        huhanshang.Broadcast();
    }

}

 

设计模式11---观察者模式

标签:des   style   blog   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/deman/p/4157983.html

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