java回調機制的使用

定義一個callBack接口:用於實現回調過程

public interface callBack {


    public void execute(List<NodeObj> nodeList, Collection<String> curList, List<NodeObj> tempNodes, int count);

}

控制流程實現callBack接口:

public class processController implements callBack {

    File file = null;
    int count = 0;
    Collection<String> curList = null;
    List<NodeObj> nodeList = null;
    List<NodeObj> tempNodes = null;
    callBack callBack ;

    public processController() {
        super();
    }
    public processController(File file){
        this.file = file;
    }


    public void init(){
        //得到節點結合nodeList表示未遍歷的節點
        nodeList = new ArrayList<NodeObj>();
        //利用工具類將文件中的每個節點存儲爲NodeObj對象並返回所有這類對象的集合
        nodeList = XmlUtils.getNodes(file);
        //初始化已運行節點集合curList 該集合用來判斷節點是否可以執行
        curList = new ArrayList<String>();
        //將開始節點的id加入curList當做已經執行
        curList.add(nodeList.get(0).getAlg_id());
        //移除已經執行的節點
        nodeList.remove(0);
        //添加count臨時變量判斷輸出條件  count又來記錄未執行的節點個數
        count = nodeList.size()-1;
        //
        tempNodes = new ArrayList<NodeObj>();

        callBack = new processController();
    }

    public void algProcess(){

        //還剩餘的節點數
        if( count >=  0 ){
            //首先移除已遍歷的節點
            if(tempNodes!=null && nodeList!=null){
                nodeList.removeAll(tempNodes);
                tempNodes = new ArrayList<NodeObj>();
            }

            //若集合中還有節點則繼續流程
            algExecute alg = new algExecute();
            alg.setCallback(callBack);
            alg.algExe(nodeList, curList, tempNodes, count);
        }
    }

    @Override
    public void execute(List<NodeObj> nodeList,Collection<String> curList, List<NodeObj> tempNodes, int count) {

        this.nodeList = nodeList;
        this.count = count;
        this.curList = curList;
        this.tempNodes = tempNodes;
        algProcess();//回調函數

    }

}

主遍歷流程:

public class algExecute {

    callBack callback = new processController();

    public void setCallback(callBack callback){

        this.callback = callback;
    }

    public void algExe(List<NodeObj> nodeList, Collection<String> curList, List<NodeObj> tempNodes, int count) {

        // 判斷每個節點是否可以執行
        for (NodeObj node : nodeList) {// 11,7,5,3,1

            String[] nodeArr = node.getPrior_node().split(",");
            // 當前節點的前驅節點集合
            Collection<String> priorCol = Arrays.asList(nodeArr);
            // 如果當前節點的前驅節點已經在curList中表示可以執行該節點
            if (curList.containsAll(priorCol)) {
                // 執行
                algExe.execute(node.getAlg_id() + "||" + node.getAlg_name());
                // 將該節點的ID加入curList
                curList.add(node.getAlg_id());

                System.out.println("\t\t當前curList中的元素:" + curList.toString());
                // 將該節點加入tempNodes集合表示執行過
                tempNodes.add(node);
                // 用作判斷跳出循環條件
                count--;
            }

        }

        //通知控制算法已完成一次遍歷
        callback.execute(nodeList,curList,tempNodes,count);

    }
}

測試類:

public class TestDemo {

    public static void main(String[] args) {

        processController process = new processController(new File("./src/AlgProcess.xml"));
        process.init();
        process.algProcess();
    }

}

測試樣例:

這裏寫圖片描述

輸出結果:

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章