本文主要是介绍设计模式--观察者,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
JDK自带观察者实现
package designpattern.observable.jdk;import java.util.Observable;/*** 发布者就是目标*/
public class Publisher extends Observable {private int number;public int getNumber() {return number;}public void setNumber(int number) {this.number=number;if (this.number>10) {setChanged(); //改变通知者的状态notifyObservers(number); //调用父类Observable方法,通知所有观察者}}}
package designpattern.observable.jdk;import java.util.Observable;
import java.util.Observer;public class Subscribe1 implements Observer {public Subscribe1(Observable o) {o.addObserver(this); //将该观察者放入待通知观察者里}@Overridepublic void update(Observable o, Object arg) {System.out.println("Subscribe1收到通知:" + ((Publisher)o).getNumber()+"--"+(int)arg);}
}
package designpattern.observable.jdk;import java.util.Observable;
import java.util.Observer;/*** 订阅者就是被观察者*/
public class Subscribe implements Observer {public Subscribe(Observable o) {o.addObserver(this); //将该观察者放入待通知观察者里}@Overridepublic void update(Observable o, Object arg) {System.out.println("Subscribe收到通知:" + ((Publisher)o).getNumber()+"--"+(int)arg);}
}
package designpattern.observable.jdk;public class Test {public static void main(String[] args){Publisher publisher=new Publisher();Subscribe subscribe=new Subscribe(publisher);Subscribe1 subscribe1=new Subscribe1(publisher);publisher.setNumber(11);}
}
业务实现观察者
package designpattern.observable.biz;public interface MessasgeListener {public void reveiveMessage(MessageEvent event);
}
package designpattern.observable.biz;public abstract class AbstractMessageListener implements MessasgeListener {@Overridepublic void reveiveMessage(MessageEvent event) {//统一日志打印,异常补货,消息幂等,回滚等处理handleReveiveMessage(event);}abstract void handleReveiveMessage(MessageEvent event);
}
package designpattern.observable.biz;import java.util.ArrayList;
import java.util.List;public class GoodsMessageListener extends AbstractMessageListener {private static List<MessageEventHandler> messageEventHandlerList=new ArrayList<>();static {messageEventHandlerList.add(new PaySuccessMessageEventHandler());messageEventHandlerList.add(new WairPayEveneHandler());}@Overridevoid handleReveiveMessage(MessageEvent event) {for(MessageEventHandler messageEventHandler:messageEventHandlerList){if(messageEventHandler.canHandle(event)){messageEventHandler.execute(event);}}}
}
package designpattern.observable.biz;public class MessageEvent {String topic;String eventCode;public String getTopic() {return topic;}public void setTopic(String topic) {this.topic = topic;}public String getEventCode() {return eventCode;}public void setEventCode(String eventCode) {this.eventCode = eventCode;}@Overridepublic String toString() {return "MessageEvent{" +"topic='" + topic + '\'' +", eventCode='" + eventCode + '\'' +'}';}
}
package designpattern.observable.biz;public interface MessageEventHandler {boolean canHandle(MessageEvent messageEvent);void execute(MessageEvent messageEvent);
}
package designpattern.observable.biz;public class WairPayEveneHandler implements MessageEventHandler {@Overridepublic boolean canHandle(MessageEvent messageEvent) {return "waitpay".equals(messageEvent.getTopic());}@Overridepublic void execute(MessageEvent messageEvent) {System.out.println(messageEvent);}
}
package designpattern.observable.biz;public class PaySuccessMessageEventHandler implements MessageEventHandler {@Overridepublic boolean canHandle(MessageEvent messageEvent) {return "paysuccess".equals(messageEvent.getTopic());}@Overridepublic void execute(MessageEvent messageEvent) {System.out.println(messageEvent);}
}
package designpattern.observable.biz;public class Test {public static void main(String[] args){GoodsMessageListener goodsMessageListener=new GoodsMessageListener();MessageEvent event=new MessageEvent();event.setTopic("paysuccess");goodsMessageListener.handleReveiveMessage(event);}
}
这篇关于设计模式--观察者的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!