{"id":153206,"date":"2019-08-20T08:10:03","date_gmt":"2019-08-20T00:10:03","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=153206"},"modified":"2019-08-08T16:11:21","modified_gmt":"2019-08-08T08:11:21","slug":"java-observer","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/java-observer.html","title":{"rendered":"Java\u8bbe\u8ba1\u6a21\u5f0f\u4e4b\u4e00\uff1aObserver\u89c2\u5bdf\u8005\u6a21\u5f0f"},"content":{"rendered":"
Observer\u89c2\u5bdf\u8005\u6a21\u5f0f\u76ee\u5f55<\/strong><\/span><\/div>\n
\r\n1 \u89c2\u5bdf\u8005\u63a5\u53e3;\r\n2 \u88ab\u89c2\u5bdf\u8005;\r\n3 \u89c2\u4f17\u7c7b;\r\n4 \u7535\u5f71\u7c7b;\r\n5 \u6548\u679c\u5982\u4e0b;\r\n<\/pre>\n
\u89c2\u5bdf\u8005\u63a5\u53e3<\/strong><\/span><\/div>\n
\r\n     package ObserverModel;\r\n    \/**\r\n     * \u89c2\u5bdf\u8005\uff0c\u5b9a\u4e49update\u65b9\u6cd5\uff0c\u610f\u4e3a\u63a5\u6536\u4ece\u88ab\u89c2\u5bdf\u8005\u90a3\u91cc\u53cd\u9988\u7684\u4fe1\u606f\r\n    *\/\r\n    public interface Observer {\r\n        \/**\r\n         * @param message \u63a5\u6536\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b\uff0c\u8fd9\u91cc\u662fString\r\n         *\/\r\n    void update(String message);\r\n    }\r\n<\/pre>\n
\u88ab\u89c2\u5bdf\u8005<\/strong><\/span><\/div>\n
\r\n    package ObserverModel;\r\n    \/**\r\n     * \u62bd\u8c61\u63a5\u53e3\u88ab\u89c2\u5bdf\u8005\uff0c\u6bd4\u5982\u5fae\u4fe1\u516c\u4f17\u53f7\u6216\u8005\u7535\u5f71\u7b49\r\n     *\/\r\n    public interface Observerable {\r\n        \/**\r\n         * \u6ce8\u518c\u4e00\u4e2a\u65b0\u7684\u89c2\u5bdf\u8005\r\n         *\r\n         * @param observer \u89c2\u5bdf\u8005\r\n         *\/\r\n        void registerObserver(Observer observer);\r\n        \/**\r\n         * \u5220\u9664\u89c2\u5bdf\u8005\r\n         *\r\n         * @param observer \u89c2\u5bdf\u8005\r\n         *\/\r\n        void removeObserver(Observer observer);\r\n        \/**\r\n         * \u901a\u77e5\u89c2\u5bdf\u8005\r\n         *\/\r\n        void notifyObserver();\r\n    }\r\n<\/pre>\n
\u89c2\u4f17\u7c7b<\/strong><\/span><\/div>\n
\r\n    package ObserverModel;\r\n    \/**\r\n     * \u89c2\u4f17 \u5b9e\u73b0\u89c2\u5bdf\u8005\u63a5\u53e3\r\n     *\/\r\n    public class Audience implements Observer {\r\n        \/\/\u540d\u5b57\r\n        private String name;\r\n        \/\/\u6536\u5230\u7684\u4fe1\u606f\r\n        private String message;\r\n        Audience(String name) {\r\n            this.name = name;\r\n        }\r\n        @Override\r\n        public void update(String message) {\r\n            this.message = message;\r\n            watch(message);\r\n        }\r\n        \/**\r\n         * \u6253\u5370\u88ab\u89c2\u5bdf\u8005\u53d1\u51fa\u7684\u4fe1\u606f\r\n         *\r\n         * @param information \u4fe1\u606f\r\n         *\/\r\n        void watch(String information) {\r\n            System.out.println(name + \"\u770b\u5230\u4e86\uff1a\" + message);\r\n        }\r\n    }\r\n<\/pre>\n
\u7535\u5f71\u7c7b<\/strong><\/span><\/div>\n
\r\n    package ObserverModel;\r\n    import java.util.ArrayList;\r\n    import java.util.List;\r\n    \/**\r\n     * \u7535\u5f71\u7c7b\r\n     *\/\r\n    public class Movie implements Observerable {\r\n        \/**\r\n         * \u89c2\u4f17\uff0c\u8fd9\u91cc\u5c31\u662f\u89c2\u5bdf\u8005\r\n         *\/\r\n        private List observers;\r\n        Movie() {\r\n            this.observers = new ArrayList<>();\r\n        }\r\n        \/**\r\n         * \u88ab\u89c2\u5bdf\u8005\u4f20\u9012\u51fa\u7684\u4fe1\u606f\uff0c\u6bd4\u5982\u5267\u60c5\uff0c\u753b\u9762\u7b49\r\n         *\/\r\n        private String information;\r\n        \/**\r\n         * \u6709\u4eba\u6765\u770b\u7535\u5f71\uff0c\u6dfb\u52a0\u4e00\u4e2a\u89c2\u5bdf\u8005\r\n         *\r\n         * @param observer \u89c2\u5bdf\u8005\r\n         *\/\r\n        @Override\r\n        public void registerObserver(Observer observer) {\r\n            observers.add(observer);\r\n        }\r\n        \/**\r\n         * \u89c2\u4f17\u8d70\u4e86\uff0c\u4ece\u89c2\u4f17\u4e2d\u79fb\u9664\r\n         *\r\n         * @param observer \u89c2\u5bdf\u8005\r\n         *\/\r\n        @Override\r\n        public void removeObserver(Observer observer) {\r\n            if (observers.size() > 0) {\r\n                observers.remove(observer);\r\n            }\r\n        }\r\n        \/**\r\n         * \u8fd9\u91cc\u7684notifyObserver\u53ef\u4ee5\u7406\u89e3\u4e3a\u5411\u89c2\u5bdf\u8005\u4f20\u9012\u4fe1\u606f\uff0c\u6bd4\u5982\u770b\u7535\u5f71\u5c31\u662f\u518d\u4ece\u7535\u5f71\u4e2d\u83b7\u53d6\u4fe1\u606f\r\n         *\/\r\n        @Override\r\n        public void notifyObserver() {\r\n            observers.forEach(observer -> {\r\n                observer.update(information);\r\n            });\r\n        }\r\n        \/**\r\n         * \u88ab\u89c2\u5bdf\u8005\u751f\u4ea7\u6d88\u606f\u65f6\uff0c\u7ed9\u6240\u6709\u89c2\u5bdf\u8005\u53d1\u9001\u901a\u77e5\r\n         * @param information\r\n         *\/\r\n        public void setInformation(String information) {\r\n            this.information = information;\r\n            notifyObserver();\r\n        }\r\n    }\r\n<\/observer><\/pre>\n
\u6548\u679c\u5982\u4e0b<\/strong><\/span><\/div>\n
\r\n    package ObserverModel;\r\n    public class MainTest {\r\n        public static void main(String[] args) {\r\n            \/\/\u6cf0\u5766\u5c3c\u514b\u53f7\r\n            Movie titanic = new Movie();\r\n            Observer zhangsan = new Audience(\"\u5f20\u4e09\");\r\n            Observer lisi = new Audience(\"\u674e\u56db\");\r\n            Observer wangwu = new Audience(\"\u738b\u4e94\");\r\n            titanic.registerObserver(zhangsan);\r\n            titanic.registerObserver(lisi);\r\n            titanic.registerObserver(wangwu);\r\n            titanic.setInformation(\"jack is dead!\");\r\n            \/\/oh no ,\u592a\u8650\u4e86\uff0c\u4e0d\u770b\u4e86\uff01\r\n            titanic.removeObserver(wangwu);\r\n            titanic.setInformation(\"rose not dead!\");\r\n        }\r\n    \/\/    \u5f20\u4e09\u770b\u5230\u4e86\uff1ajack is dead!\r\n    \/\/    \u674e\u56db\u770b\u5230\u4e86\uff1ajack is dead!\r\n    \/\/    \u738b\u4e94\u770b\u5230\u4e86\uff1ajack is dead!\r\n    \/\/    \u5f20\u4e09\u770b\u5230\u4e86\uff1arose not dead!\r\n    \/\/    \u674e\u56db\u770b\u5230\u4e86\uff1arose not dead!\r\n    }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

1 \u89c2\u5bdf\u8005\u63a5\u53e3; 2 \u88ab\u89c2\u5bdf\u8005; 3 \u89c2\u4f17\u7c7b; 4 \u7535\u5f71\u7c7b; 5 \u6548\u679c\u5982\u4e0b; package Observer […]<\/p>\n","protected":false},"author":1479,"featured_media":45954,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-153206","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/153206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=153206"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/153206\/revisions"}],"predecessor-version":[{"id":153242,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/153206\/revisions\/153242"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/45954"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=153206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=153206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=153206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}