1. 程式人生 > >java 設計模式之責任鏈模式的幾種寫法

java 設計模式之責任鏈模式的幾種寫法

寫法1:

package com.huang.test.designmodel;

/**
 * java 設計模式:責任鏈模式
 */
public class ChainOfResponsibilityPatternTest {
    public static final int LOG_CONSOLE = 1;
    public static final int LOG_ERROR = 2;
    public static final int LOG_FILE = 3;

    class BaseLog
    {
        private int level;
        private BaseLog nextLog;

        public BaseLog(int level, BaseLog nextLog)
        {
            this.level = level;
            this.nextLog = nextLog;
        }

        public void logout(int loglevel, String message) {
            if(loglevel == level)
            {
                printMessage(message);
            }else
            {
                BaseLog next = nextLog;
                if(next != null)
                {
                    next.logout(loglevel, message);
                }
            }
        }

        public void printMessage(String message) {

        }
    }

    private class ConsoleLog extends BaseLog
    {
        public ConsoleLog() {
            super(LOG_CONSOLE, null);
        }

        @Override
        public void printMessage(String message) {
            System.out.println("Console:" + message);
        }
    }

    private class ErrorLog extends BaseLog
    {
        public ErrorLog() {
            super(LOG_ERROR, new ConsoleLog());
        }

        @Override
        public void printMessage(String message) {
            System.out.println("Error:" + message);
        }
    }

    private class FileLog extends BaseLog
    {
        public FileLog() {
            super(LOG_FILE, new ErrorLog());
        }

        @Override
        public void printMessage(String message) {
            System.out.println("File:" + message);
        }
    }

    public BaseLog getLog()
    {
        return new FileLog();
    }

    private void test()
    {
        BaseLog log = getLog();
        log.logout(LOG_ERROR,  "fuck you");
    }

    public static void main(String[] args)
    {
        ChainOfResponsibilityPatternTest crpt = new ChainOfResponsibilityPatternTest();
        crpt.test();
    }
}

寫法2:

package com.huang.test.designmodel;

import com.huang.test.util.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * java 責任鏈模式:
 *
 */
public class ChainOfResponsibilityPatternTest2 {
    class Result
    {
        private String result;
        public Result(String res)
        {
            this.result = res;
        }

        public String getResult() {
            return result;
        }

        public void setResult(String result) {
            this.result = result;
        }

        @Override
        public String toString() {
            return "result:" + result;
        }
    }

    interface Intecepter
    {
        public Result intercept(IntecepterChain chain);
    }

    class Intecepter1 implements Intecepter
    {
        @Override
        public Result intercept(IntecepterChain chain) {
            Tools.log("inte1 before...");
            Result res = null;//chain.proceed(chain.request);
            Tools.log("inte1 after...");
            return res;
        }
    }

    class Intecepter2 implements Intecepter
    {
        @Override
        public Result intercept(IntecepterChain chain) {
            Tools.log("inte2 before...");
            Result res = chain.proceed(chain.request);
            Tools.log("inte2 after...");
            return res;
        }
    }

    class Intecepter3 implements Intecepter
    {
        @Override
        public Result intercept(IntecepterChain chain) {
            Tools.log("inte3 before...");
            Result res = new Result("inte3");
            Tools.log("inte3 after...");
            return res;
        }
    }

    class IntecepterChain
    {
        private List<Intecepter> intecepters = new ArrayList<>();
        private int index;
        private String request;
        public IntecepterChain(List<Intecepter> intecepters, int index, String request)
        {
            this.intecepters = intecepters;
            this.index = index;
            this.request = request;
        }

        public Result proceed(String request)
        {
            if(index >= intecepters.size())
            {
                throw new IllegalArgumentException("index cannot ge than intecepters's size!");
            }

            IntecepterChain next = new IntecepterChain(intecepters, index + 1, request);
            Intecepter intecep = intecepters.get(index);
            Result response = intecep.intercept(next);

            return response;
        }
    }

    public void runChain()
    {
        String request = "do request...";
        List<Intecepter> intecepters = new ArrayList<>();
        intecepters.add(new Intecepter1());
        intecepters.add(new Intecepter2());
        intecepters.add(new Intecepter3());

        IntecepterChain chain = new IntecepterChain(intecepters, 0, request);
        Result res = chain.proceed(request);
        Tools.log("" + res);
    }

    private void test() {
        runChain();
    }

    public static void main(String[] args)
    {
        ChainOfResponsibilityPatternTest2 it = new ChainOfResponsibilityPatternTest2();
        it.test();
    }
}