JAVA设计模式之观察者模式

JAVA设计模式之观察者模式

回复
  • 李逍遥用户
    [[{"txt": "JAVA\u8bbe\u8ba1\u6a21\u5f0f\u4e4b\u89c2\u5bdf\u8005\u6a21\u5f0f"}], [{"txt": ["\u5de5\u5177/\u539f\u6599"]}, {"txt": "java"}, {"txt": "eclipse"}], [{"txt": ["\u65b9\u6cd5/\u6b65\u9aa4"]}, {"txt": "\u89c2\u5bdf\u8005\u8bbe\u8ba1\u6a21\u5f0f\u6a21\u62df\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u610f\u601d\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u89e6\u5373\u53d1\u3002\u5982\u679c\u8981\u60f3\u5b9e\u73b0\u89c2\u5bdf\u8005\u7684\u8bbe\u8ba1\u6a21\u5f0f\u9700\u8981\u4ee5\u4e0b\u7684\u4e24\u4e2a\u7a0b\u5e8f\u7ed3\u6784\uff1a\u89c2\u5bdf\u8005\uff1ajava.util.Observer;\u88ab\u89c2\u5bdf\u8005\uff1ajava.util.Observable;\u8303\u4f8b\uff1a\u5b9e\u73b0\u89c2\u5bdf\u8005\uff1a//\u8fd9\u4e9b\u662f\u6240\u6709\u7684\u89c2\u5bdf\u8005class Person implements Observer {\u00a0 \u00a0 \u00a0 \u00a0 @Override\u00a0 \u00a0 \u00a0 \u00a0 public void update(Observable o, Object arg) {\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 //\u4e00\u65e6\u4f60\u5173\u6ce8\u7684\u4e8b\u60c5\u53d1\u751f\u4e86\u53d8\u5316\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if(o instanceof House) {\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if(arg instanceof Double) {\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 System.out.println(\"\u623f\u4ef7\u4e0a\u6da8\uff01\u65b0\u4ef7\u683c\uff1a\" + arg);\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0 \u00a0 \u00a0 \u00a0\u00a0}"}, {"img": ["https://exp-picture.cdn.bcebos.com/e076d77622bc7dc510fda5ec5e460596b9142932.m2m?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_500%2Climit_1"]}, {"txt": "\u5b9e\u73b0\u88ab\u89c2\u5bdf\u8005\uff1a\u5f53\u623f\u4ef7\u4e0a\u6da8\u65f6\u901a\u77e5\u89c2\u5bdf\u8005\u3002class House extends Observable {\u00a0 \u00a0 \u00a0 \u00a0 private double price;\u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 public House(double price) {\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 this.price\u00a0 = price;\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 public void setPrice(double price) {\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if(price > this.price) {\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 super.setChanged(); //\u73b0\u5728\u5173\u6ce8\u7684\u4ef7\u683c\u6539\u53d8\u4e86\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 super.notifyObservers(price); //\u5524\u9192\u6240\u6709\u7684\u89c2\u5bdf\u8005\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 this.price = price;\u00a0 \u00a0 \u00a0 \u00a0 }}"}, {"img": ["https://exp-picture.cdn.bcebos.com/47bf594ec28333bfd075bbfcdbb8b43ea9db1d32.m2m?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_500%2Climit_1"]}, {"txt": "\u5728\u5ba2\u6237\u7aef\u6ce8\u518c\u89c2\u5bdf\u8005\uff1apublic class ObserverDesign {\u00a0 \u00a0 \u00a0 \u00a0 public static void main(String[] args) {\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Person personA = new Person();\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Person personB = new Person();\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Person personC = new Person();\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 //\u8bbe\u7f6e\u89c2\u5bdf\u8005\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 House house = new House(8000000);\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 house.addObserver(personC);\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 house.addObserver(personB);\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 house.addObserver(personA);\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 house.setPrice(9000000);\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0 \u00a0 \u00a0 \u00a0\u00a0}"}, {"img": ["https://exp-picture.cdn.bcebos.com/560be432939c2cf72b2852aa452c5b1b1fde1232.m2m?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_500%2Climit_1"]}, {"txt": "\u5f53\u623f\u4ef7\u4e0a\u6da8\u65f6\u6267\u884cmain\u4e3b\u65b9\u6cd5\u89c2\u5bdf\u8005\u4f1a\u6536\u5230\u901a\u77e5\uff1a"}, {"img": ["https://exp-picture.cdn.bcebos.com/1f9feadca039131f32765d6ae275f2c4ed990a32.m2m?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_500%2Climit_1"]}, {"txt": "\u5f53\u623f\u4ef7\u4e0b\u964d\u6267\u884cmain\u4e3b\u65b9\u6cd5\u89c2\u5bdf\u8005\u4e0d\u4f1a\u6536\u5230\u901a\u77e5\uff1apackage com.gwolf.design;import java.util.Observable;import java.util.Observer;//\u8fd9\u4e9b\u662f\u6240\u6709\u7684\u89c2\u5bdf\u8005class Person implements Observer {\u00a0 \u00a0 \u00a0 \u00a0 @Override\u00a0 \u00a0 \u00a0 \u00a0 public void update(Observable o, Object arg) {\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 //\u4e00\u65e6\u4f60\u5173\u6ce8\u7684\u4e8b\u60c5\u53d1\u751f\u4e86\u53d8\u5316\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if(o instanceof House) {\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if(arg instanceof Double) {\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 System.out.println(\"\u623f\u4ef7\u4e0a\u6da8\uff01\u65b0\u4ef7\u683c\uff1a\" + arg);\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0 \u00a0 \u00a0 \u00a0\u00a0}//\u88ab\u89c2\u5bdf\u8005class House extends Observable {\u00a0 \u00a0 \u00a0 \u00a0 private double price;\u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 public House(double price) {\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 this.price\u00a0 = price;\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 public void setPrice(double price) {\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if(price > this.price) {\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 super.setChanged(); //\u73b0\u5728\u5173\u6ce8\u7684\u4ef7\u683c\u6539\u53d8\u4e86\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 super.notifyObservers(price); //\u5524\u9192\u6240\u6709\u7684\u89c2\u5bdf\u8005\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 this.price = price;\u00a0 \u00a0 \u00a0 \u00a0 }}public class ObserverDesign {\u00a0 \u00a0 \u00a0 \u00a0 public static void main(String[] args) {\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Person personA = new Person();\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Person personB = new Person();\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Person personC = new Person();\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 //\u8bbe\u7f6e\u89c2\u5bdf\u8005\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 House house = new House(8000000);\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 house.addObserver(personC);\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 house.addObserver(personB);\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 house.addObserver(personA);\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 house.setPrice(7000000);\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0 \u00a0 \u00a0 \u00a0\u00a0}"}, {"img": ["https://exp-picture.cdn.bcebos.com/58021a0148fe1e4258f08a62c2299a8838130332.m2m?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_500%2Climit_1"]}, {"txt": "\u6267\u884cmain\u4e3b\u65b9\u6cd5\uff0c\u6211\u4eec\u5c06\u770b\u5230\u63a7\u5236\u53f0\u4e0d\u4f1a\u6709\u4efb\u4f55\u8f93\u51fa\u3002"}, {"img": ["https://exp-picture.cdn.bcebos.com/9bc92713e8e5eceea127309ccc704618deda7a32.m2m?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_500%2Climit_1"]}]]
    2024-11-24
    1楼
    回复

参与评论

游客评论不支持回复他人评论内容,如需回复他人评论内容请