设计模式--观察者

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);        //将该观察者放入待通知观察者里
    }
    @Override
    public 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);        //将该观察者放入待通知观察者里
    }
    @Override
    public 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 {


    @Override
    public 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 messageEventHandlerList=new ArrayList<>();
    static {
        messageEventHandlerList.add(new PaySuccessMessageEventHandler());
        messageEventHandlerList.add(new WairPayEveneHandler());

    }

    @Override
    void 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;
    }

    @Override
    public 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 {

    @Override
    public boolean canHandle(MessageEvent messageEvent) {
        return "waitpay".equals(messageEvent.getTopic());
    }

    @Override
    public void execute(MessageEvent messageEvent) {
      System.out.println(messageEvent);
    }
}
package designpattern.observable.biz;

public class PaySuccessMessageEventHandler implements MessageEventHandler {

    @Override
    public boolean canHandle(MessageEvent messageEvent) {
        return "paysuccess".equals(messageEvent.getTopic());
    }

    @Override
    public 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);
    }
}

 

你可能感兴趣的:(设计模式)