Java用观察者模式重构复杂的业务代码

1. 定义一个统一的回调接口

public interface CallerInter {
    void call(Param param);
}


2. 定义观察者接口

public interface ObserverInter{
    /**
     * 增加观察者
     *
     * @param callInter
     */
    void addObserver(CallerInter callInter);

    /**
     * 删除观察者
     *
     * @param lemmaWriteBackOpObserver
     */
    void delObserver(CallerInter callInter);

    /**
     * 通知观察者
     *
     * @param lemmaRecord
     */
    void notifyAllObserver(Param param);
}

3. 定义观察者实现类

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by denglinjie on 2016/8/19.
 */
@Service
public class ObserverImpl implements ObserverInter, InitializingBean {

    //要通知的对象列表
    private List<CallerInter> callerInterList;

    public void addObserver(CallerInter callInter) {
        callerInterList.add(callInter);
    }

    public void delObserver(CallerInter callInter) {
        callerInterList.remove(callInter);
    }

    public void notifyAllObserver(Param param) {
        for(CallerInter callerInter : callerInterList) {
            callerInter.call(param);
        }
    }

    public void afterPropertiesSet() throws Exception {
        callerInterList = new CopyOnWriteArrayList<CallerInter>();  //这里用一个线程安全的list,因为可能多线程同时操作它
    }

    /**
     * 对外提供一个接口,当这个接口被调用的时候,可以通知所有注册进来的对象的特定call接口
     */
    public void operateService() {
        //构造参数
        Param param = new Param();
        notifyAllObserver(param);
    }
}

4. 定义两个要被观察的对象,需要把自己注册到观察者列表中,即上述ObserImpl的callInterList集合中

import com.sogou.study.observer.CallerInter;
import com.sogou.study.observer.ObserverInter;
import com.sogou.study.observer.Param;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by denglinjie on 2016/8/19.
 */
public class CallImplFirst implements CallerInter, InitializingBean {

    @Autowired
    private ObserverInter observerInter;

    public void call(Param param) {
        //do something
    }

    public void afterPropertiesSet() throws Exception {
        observerInter.addObserver(this);
    }
}

import com.sogou.study.observer.CallerInter;
import com.sogou.study.observer.ObserverInter;
import com.sogou.study.observer.Param;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by denglinjie on 2016/8/19.
 */
public class CallImplSecond implements CallerInter, InitializingBean {

    @Autowired
    private ObserverInter observerInter;

    public void call(Param param) {
        //do something
    }

    public void afterPropertiesSet() throws Exception {
        observerInter.addObserver(this);
    }
}

5. 这样其他业务调用ObserverImpl的operateService方法时,就可以通知所有的注册的对象做一些事情。当一个业务逻辑很复杂要做很多事情的时候,如果把代码写在一起会显得很拥挤,不利于维护,这时,可以通过这种观察者模式,将不同的业务处理区分开。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值