美文网首页
设计模式-责任链设计模式

设计模式-责任链设计模式

作者: woochen123 | 来源:发表于2017-11-23 14:26 被阅读0次

    1.定义

    使每个对象都有机会处理请求,把各个对象连成一条链,并沿着这条链处理该请求,直到有对象处理他为止

    2.示例

    /**
     *迭代器
     *@author woochen123
     *@time 2017/11/23 9:18
     *@desc
     */
    
    public interface Iterator<T> {
        T next();
        boolean hasNext();
    }
    
    /**
     *qq迭代器
     *@author woochen123
     *@time 2017/11/23 9:20
     *@desc
     */
    
    public class QQIterator implements Iterator<UserBean> {
    
        List<UserBean> list;
        int index;
    
        public QQIterator(List<UserBean> list) {
            this.list = list;
        }
    
        @Override
        public UserBean next() {
            return list.get(index++);
        }
    
        @Override
        public boolean hasNext() {
            return index < list.size();
        }
    
    }
    
    /**
     *微信迭代器
     *@author woochen123
     *@time 2017/11/23 9:19
     *@desc
     */
    public class WxIterator implements Iterator<UserBean> {
        UserBean[] array;
        int index;
    
        public WxIterator(UserBean[] array) {
            this.array = array;
        }
    
        @Override
        public UserBean next() {
            return array[index++];
        }
    
        @Override
        public boolean hasNext() {
            return index < array.length;
        }
    
    }
    
    /**
     *用户系统连接功能
     *@author chenwuchao
     *@time 2017/11/23 10:42
     *@desc
     */
    public interface IUserSystemHandler<T extends IUserSystemHandler> {
        /**
         * 下一个用户系统
         */
        void nextHandler(T iUserSystemHandler);
    }
    
    /**
     *用户系统抽象管理类
     *@author chenwuchao
     *@time 2017/11/23 13:36
     *@desc
     */
    public abstract class AbrastractUserSystemHandler implements IUserSystemHandler<AbrastractUserSystemHandler>, IUserSystem, Aggregate {
        private AbrastractUserSystemHandler iUserSystemHandler;
    
        @Override
        public void nextHandler(AbrastractUserSystemHandler iUserSystemHandler) {
            this.iUserSystemHandler = iUserSystemHandler;
        }
    
        public AbrastractUserSystemHandler getiUserSystemHandler() {
            return iUserSystemHandler;
        }
    
        @Override
        public UserBean queryInfo(UserBean userBean) {
            Iterator<UserBean> interator = interator();
            while (interator.hasNext()) {
                UserBean next = interator.next();
                System.out.println("查询记录:" + next.getName() + " " + next.getPwd());
                if (TextUtils.equals(next.getName(), userBean.getName()) && TextUtils.equals(next.getPwd(), userBean.getPwd())) {
                    return next;
                }
            }
            //没有就交给下一个
            AbrastractUserSystemHandler nextHandler = getiUserSystemHandler();
            if (nextHandler != null) {
                return nextHandler.queryInfo(userBean);
            }
            return null;
        }
    
    }
    
    /**
     *
     *@author woochen123
     *@time 2017/11/23 9:21
     *@desc
     */
    public interface Aggregate<T> {
        /**
         * 公共接口
         * @return 返回迭代器
         */
        Iterator<T> interator();
    }
    
    /**
     *查询用户信息
     *@author woochen123
     *@time 2017/11/23 10:57
     *@desc
     */
    public interface IUserSystem<T extends UserBean> {
        /**
         * 查询
         */
         T queryInfo(T userBean);
    }
    
    /**
     * qq登录系统
     * @author chenwuchao
     * @time 2017/11/22 17:14
     * @desc
     */
    public class QQUserSystem extends AbrastractUserSystemHandler {
        List<UserBean> list;
    
        public QQUserSystem() {
            list = new ArrayList<>();
            list.add(new UserBean("小明", "12"));
            list.add(new UserBean("小红", "13"));
            list.add(new UserBean("小刚", "14"));
        }
    
        public List<UserBean> elements() {
            return list;
        }
    
        @Override
        public Iterator<UserBean> interator() {
            return new QQIterator(list);
        }
    
    
    /**
     *微信登录系统
     *@author woochen123
     *@time 2017/11/23 9:23
     *@desc
     */
    public class WxUserSystem extends AbrastractUserSystemHandler {
        private UserBean[] array ;
    
        public WxUserSystem() {
            array = new UserBean[3];
            array[0] = new UserBean("小花","12");
            array[1] = new UserBean("小华","13");
            array[2] = new UserBean("小方","14");
        }
    
        public UserBean[] elements() {
            return array;
        }
    
        @Override
        public Iterator<UserBean> interator() {
            return new WxIterator(array);
        }
    }
    
    public class Client {
        private QQUserSystem qqUserSystem;
        private WxUserSystem wxUserSystem;
    
        public void main() {
            UserBean userBean = new UserBean("小花", "12");
            qqUserSystem = new QQUserSystem();
            wxUserSystem = new WxUserSystem();
            qqUserSystem.nextHandler(wxUserSystem);
            UserBean userBean1 = qqUserSystem.queryInfo(userBean);
            if(userBean1 == null){
                System.out.println("登录失败!" + userBean.getName() + "   "+ userBean.getPwd());
            }else {
                System.out.println("登录成功!" + userBean.getName() + "   "+ userBean.getPwd());
            }
        }}
    

    3.应用

    View的事件分发机制

    相关文章

      网友评论

          本文标题:设计模式-责任链设计模式

          本文链接:https://www.haomeiwen.com/subject/lswovxtx.html