美文网首页
观察者模式

观察者模式

作者: Davisxy | 来源:发表于2019-05-07 11:57 被阅读0次

    小栗子

    package com.principle.observer;
    
    public interface Observer {
        void update(Subject subject);
    }
    
    package com.principle.observer;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Subject {
        private List<Observer> list = new ArrayList<>();
    
        public void register(Observer obs) {
            list.add(obs);
        }
    
        public void remobeObserver(Observer obs) {
            list.add(obs);
        }
    
        // 通知所有的观察者更新状态
        public void notifyAllObserver() {
            for (Observer observer : list) {
                observer.update(this);
            }
        }
    }
    
    package com.principle.observer;
    
    public class ConcreteSubject extends Subject{
        private int myState;
    
        public int getMyState() {
            return myState;
        }
    
        public void setState(int myState) {
            this.myState = myState;
            //主题对象(目标对象)值发生了变化,请通知所有的观察者
            this.notifyAllObserver();
        }
        
    }
    
    package com.principle.observer;
    
    public class ObserveA implements Observer{
        
        //myState需要跟目标对象的state值保持一致
        private int myState;
    
        @Override
        public void update(Subject subject) {
            myState=((ConcreteSubject)subject).getMyState();
        }
    
        public int getMyState() {
            return myState;
        }
    
        public void setMyState(int myState) {
            this.myState = myState;
        }
        
    }
    
    package com.principle.observer;
    
    public class Client {
        
        public static void main(String[] args) {
            //目标对象
            ConcreteSubject subject=new ConcreteSubject();
            
            //创建多个对象
            ObserveA obs1=new ObserveA();
            ObserveA obs2=new ObserveA();
            ObserveA obs3=new ObserveA();
            
            //将这三个观察者添加到subject对象的观察者队伍中
            subject.register(obs1);
            subject.register(obs2);
            subject.register(obs3);
            
            //改变subject的状态
            subject.setState(30);
            
            //观察者的状态是不是也发生了改变
            System.out.println(obs1.getMyState());
            System.out.println(obs2.getMyState());
            System.out.println(obs3.getMyState());
            
            
            
        }
    
    }
    
    控制台打印:
    30
    30
    30
    

    类图

    观察者模式.png

    可以使用JavaSE提供的:java.util.Observable / java.util.Observer

    小栗子

    package com.principle.observer2;
    
    import java.util.Observable;
    
    public class ConcreteSubject extends Observable{
        private int state;
    
        public int getState() {
            return state;
        }
    
        public void setState(int state) {
            this.state = state;
            
            //表示目标对象已经做了更改
            setChanged();
            //通知所有对象
            notifyObservers(state);
        }
        
    
    }
    
    package com.principle.observer2;
    
    import java.util.Observable;
    import java.util.Observer;
    
    public class ObserverA implements Observer{
        
        private int myState;
        
        public int getMyState() {
            return myState;
        }
    
        public void setMyState(int myState) {
            this.myState = myState;
        }
    
        @Override
        public void update(Observable o, Object arg) {
            this.myState=((ConcreteSubject)o).getState();
        }
    
    }
    
    package com.principle.observer2;
    
    public class Client {
        
        public static void main(String[] args) {
            //创建目标对象Observable
            ConcreteSubject subject=new ConcreteSubject();
            
            //创建观察者
            ObserverA obs1=new ObserverA();
            ObserverA obs2=new ObserverA();
            ObserverA obs3=new ObserverA();
            
            //将上面三个观察者添加到目标对象Ssubject的观察着容器中
            subject.addObserver(obs1);
            subject.addObserver(obs2);
            subject.addObserver(obs3);
            
            //改变subject对象的状态
            subject.setState(3000);
            
            System.out.println(obs1.getMyState());
            System.out.println(obs2.getMyState());
            System.out.println(obs3.getMyState());
            
            subject.setState(50);
            
            System.out.println(obs1.getMyState());
            System.out.println(obs2.getMyState());
            System.out.println(obs3.getMyState());
        }
    
    }
    
    控制台打印:
    3000
    3000
    3000
    50
    50
    50
    

    开发中常见的场景:

    • 聊天室程序的。服务器转发给所有客户端
    • 网络游戏(多人联机对战)场景中,服务端将客户端的状态进行分发
    • 邮件订阅
    • Servlet中,监听器的实现
    • Android中,广播机制

    相关文章

      网友评论

          本文标题:观察者模式

          本文链接:https://www.haomeiwen.com/subject/mvqznqtx.html