设计模式系列:责任链模式、命令模式

场景

大家都是coder,所以说日志对我们来说相当的重要。大家都知道级别:debug

分析

略,大家应该都不陌生

代码

public class Tester {
    public static void main(String[] args) {
        FileLogger debugLogger = new FileLogger(AbstractLogger.INFO);
        ConsoleLogger consoleLogger = new ConsoleLogger(AbstractLogger.WARN);
        ErrorLogger errorLogger = new ErrorLogger(AbstractLogger.ERROR);
        debugLogger.setNextLogger(consoleLogger);
        consoleLogger.setNextLogger(errorLogger);
        debugLogger.logMessage(AbstractLogger.WARN, "错误信息");
    }
}

public abstract class AbstractLogger {
    public final static int DEBUG = 1;
    public final static int INFO = 2;
    public final static int WARN = 3;
    public final static int ERROR = 4;
    protected int level;
    private AbstractLogger nextLogger;
    public void setNextLogger(AbstractLogger nextLogger) {
        this.nextLogger = nextLogger;
    }
    public void logMessage(int level, String message) {
        if(this.level <= level) {
            writeLog(message);
        }
        if(nextLogger != null) {
            nextLogger.logMessage(level, message);
        }
    }
    protected abstract void writeLog(String message);
}

public class ConsoleLogger extends AbstractLogger {
    public ConsoleLogger(int level) {
        this.level = level;
    }
    protected void writeLog(String message) {
        System.out.println("stand console logger:"+message);
    }
}

public class ErrorLogger extends AbstractLogger {
    public ErrorLogger(int level) {
        this.level = level;
    }
    protected void writeLog(String message) {
        System.out.println("error console level:"+message);
    }
}

public class WarnLogger extends AbstractLogger {
    public WarnLogger(int level) {
        this.level = level;
    }
    protected void writeLog(String message) {
        System.out.println("warn logger:"+message);
    }
}

定义

使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这个对象炼成一条链,并沿着这条连传递该请求,直到有一个对象处理它为止。

说明

  1. 以上其实不算是一个纯的责任链,为什么呢?因为纯的责任链不会分布执行。
  2. 责任链并不是仅是apache-commons-chain那种静态的责任链,还有动态链接。

类图

设计模式系列:责任链模式、命令模式_第1张图片
屏幕快照 2017-08-27 上午12.53.32.png

场景

我们键盘都是多少个按键?不知道吧,哈哈!一般是104个(大键盘)、87个(小键盘)。今天不说普通的字母键和数字键。我们说说使用过程中的组合键吧。control+shift+Z,打开输入法的特殊字符窗口【这个是真的】;control+shift+R,这个是打开浏览器窗口【不可思议吧,反正我的机器是这样的】等等诸如此类的。

分析

  1. control+shift+R 打开浏览器
  2. control+shift+Z 打开输入法的特殊字符窗口,注意不是打开浏览器
  3. command+shift+4 切图【哈哈,这个是mac才有的小功能】

所以,说不是同一类型的操作,切忌用一个接口定义多个行为处理哈!!

代码

public class Test {
    public static void main(String[] args) {
        CropCommand corpCommand = new CropCommand(new Window());
        OpenBrowerCommand openBrowerCommand = new OpenBrowerCommand(new Brower());
        InputSpecialCharWinCommand inputSpecialCommand = new InputSpecialCharWinCommand(new SougoInput());
        corpCommand.execute();
        openBrowerCommand.execute();
        inputSpecialCommand.execute();
    }
}

public interface ICommand {
    public void execute();
}

public class CropCommand implements ICommand {
    private Window window;
    public CropCommand(Window window) {
        this.window = window;
    }
    public void execute() {
       window.crop();
    }
}

public class Window {
    public void crop() {
        System.out.println("调用指针定位功能");
        System.out.println("桌面置灰");
    }
}

public class InputSpecialCharWinCommand implements ICommand {
    private SougoInput sougoInput;
    public InputSpecialCharWinCommand(SougoInput sougoInput){
        this.sougoInput = sougoInput;
    }
    public void execute() {
        sougoInput.openSpecialCharWin();
    }
}

public class SougoInput {
    public void openSpecialCharWin() {
        System.out.println("找到特殊字符的地址");
        System.out.println("调用PC机画出窗口");
    }
}

public class OpenBrowerCommand implements ICommand {
    private Brower brower;
    public OpenBrowerCommand(Brower brower) {
        this.brower = brower;
    }
    public void execute() {
      brower.open();
    }
}

public class Brower {
    public void open() {
        System.out.println("开启一个进程");
        System.out.println("装载本地存储记忆");
        System.out.println("打开记忆地址");
    }
}

定义

将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。

类图

设计模式系列:责任链模式、命令模式_第2张图片
屏幕快照 2017-08-27 上午12.53.00.png

你可能感兴趣的:(设计模式系列:责任链模式、命令模式)