美文网首页
2019-12-17

2019-12-17

作者: 真胖大海 | 来源:发表于2019-12-17 18:07 被阅读0次

    一.设计图

    image.png

    二.代码

    public class EventManager {
        private Map<Object, Set<Observe>> observableToObserve = new HashMap();//set集合可以去重
        private static EventManager instance;
    
        private EventManager() {
        }
    
        public static EventManager getInstance() {
            if (instance == null) {
                synchronized (EventManager.class) {
                    if (instance == null) {
                        instance = new EventManager();
                    }
                }
            }
            return instance;
        }
    
        /**
         * 完成观察者对被观察者的注册
         *
         * @param observable   事件
         * @param observe 事件的观察者
         */
        public void register(Object observable, Observe observe) {
            Set<Observe> observeSet = observableToObserve.get(observable);
            if (observeSet == null) {
                observeSet = new HashSet<>();
            }
            observeSet.add(observe);
            observableToObserve.put(observable,observeSet);
        }
    
    
        /**
         * 通知被观察者的观察者事件发生改变
         * @param observable
         */
        public void notify(Object observable) {
            Set<Observe> observes = observableToObserve.get(observable);
            if(observable!=null) {
                Iterator<Observe> iterator = observes.iterator();
                while (iterator.hasNext()) {
                    Observe observe = iterator.next();
                    observe.onObservableChange(observable);
                }
            }
        }
    
        /**
         * 取消观察者对被观察者的注册
         *
         * @param observable
         * @param observe
         */
        public void unRegister(Object observable, Object observe) {
            Set<Observe> observeList = observableToObserve.get(observable);
            if (observeList != null) {
                observeList.remove(observe);
            }
        }
    }
    
    public interface Observe {
        void onObservableChange(Object observable);
    }
    
    
    public class Test {
        public static void main(String[] args) {
            Worker worker1 = new Worker("work1");
            Worker worker2 = new Worker("work2");
            Worker worker3 = new Worker("work3");
            Boss boss = new Boss();
            boss.hireWork(worker1);
            boss.hireWork(worker2);
            boss.hireWork(worker3);
        }
    }
    
    class Worker {
        public String name;
    
        public Worker(String name) {
            this.name = name;
        }
    
        public void doingHomeWork() {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(name + " finished work");
            EventManager.getInstance().notify(this);
        }
    }
    
    class Boss implements Observe {
    
        public void hireWork(Worker worker) {
            EventManager.getInstance().register(worker, this);
            worker.doingHomeWork();
        }
    
        @Override
        public void onObservableChange(Object observable) {
            Worker worker = (Worker) observable;
            System.out.println("收到" + worker.name + "完成工作的通知");
        }
    }
    

    相关文章

      网友评论

          本文标题:2019-12-17

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