操作型模式

Template Method(模板)模式

State(状态)模式
意图在于将与状态有关的处理逻辑分散到代表对象状态的各个类中。
封装变化
操作型模式_第1张图片
public interface State {
    void push(PaintBoard paintBoard);

    void pull(PaintBoard paintBoard);

    String name();
}
public class PaintBoard {
    private State state = new RedState();
    //other methods and fields…

    public void setState(State state) {
        this.state = state;
    }

    public String getCurrentColor() {
        return state.name();
    }

    public void push() {
        state.push(this);
    }

    public void pull() {
        state.pull(this);
    }
}
public class GreenState implements State {
    @Override
    public void push(PaintBoard paintBoard) {
        paintBoard.setState(new BlueState());
    }

    @Override
    public void pull(PaintBoard paintBoard) {
        paintBoard.setState(new RedState());
    }

    @Override
    public String name() {
        return "GREEN";
    }
}
public class RedState implements State {
    @Override
    public void push(PaintBoard paintBoard) {
        paintBoard.setState(new GreenState());
    }

    @Override
    public void pull(PaintBoard paintBoard) {
        paintBoard.setState(new BlueState());
    }

    @Override
    public String name() {
        return "RED";
    }
}
public class BlueState implements State {
    @Override
    public void push(PaintBoard paintBoard) {
        paintBoard.setState(new RedState());
    }

    @Override
    public void pull(PaintBoard paintBoard) {
        paintBoard.setState(new GreenState());
    }

    @Override
    public String name() {
        return "BLUE";
    }
}
public class PaintBoardTestDrive {
    public static void main(String[] args) {
        PaintBoardTestDrive test = new PaintBoardTestDrive();
        PaintBoard paintBoard = new PaintBoard();

        System.out.println("Push Test:");
        System.out.println("Paint board current color:" + paintBoard.getCurrentColor());
        test.pushTest(paintBoard);

        System.out.println("\nPull Test:");
        System.out.println("Paint board current color:" + paintBoard.getCurrentColor());
        test.pullTest(paintBoard);
    }

    public void pushTest(PaintBoard paintBoard) {
        for (int i = 0; i < 3; i++) {
            System.out.printf("%s ---push---> ", paintBoard.getCurrentColor());
            paintBoard.push();
            System.out.println(paintBoard.getCurrentColor());
        }
    }

    public void pullTest(PaintBoard paintBoard) {
        for (int i = 0; i < 3; i++) {
            System.out.printf("%s ---pull---> ", paintBoard.getCurrentColor());
            paintBoard.pull();
            System.out.println(paintBoard.getCurrentColor());
        }
    }
}


使用enum类型
public interface State {
    void push(PaintBoard paintBoard);

    void pull(PaintBoard paintBoard);

    String name();
}
public class PaintBoard {
    private State state = Color.RED;

    public void setState(State state) {
        this.state = state;
    }

    public String getCurrentColor() {
        return state.name();
    }

    public void push() {
        state.push(this);
    }

    public void pull() {
        state.pull(this);
    }
}
public enum Color implements State {
    RED {
        @Override
        public void push(PaintBoard paintBoard) {
            paintBoard.setState(GREEN);
        }

        @Override
        public void pull(PaintBoard paintBoard) {
            paintBoard.setState(BLUE);
        }
    },
    GREEN {
        @Override
        public void push(PaintBoard paintBoard) {
            paintBoard.setState(BLUE);
        }

        @Override
        public void pull(PaintBoard paintBoard) {
            paintBoard.setState(RED);
        }
    },
    BLUE {
        @Override
        public void push(PaintBoard paintBoard) {
            paintBoard.setState(RED);
        }

        @Override
        public void pull(PaintBoard paintBoard) {
            paintBoard.setState(GREEN);
        }
    };

    @Override
    public void push(PaintBoard paintBoard) {
        throw new UnsupportedOperationException("Invalid push!");
    }

    @Override
    public void pull(PaintBoard paintBoard) {
        throw new UnsupportedOperationException("Invalid pull!");
    }
}
public class PaintBoardTestDrive {
    public static void main(String[] args) {
        PaintBoard paintBoard = new PaintBoard();
        System.out.println("Push Test:");
        System.out.println("Paint board current color:" + paintBoard.getCurrentColor());
        for (int i = 0; i < 3; i++) {
            pushTest(paintBoard);
        }

        System.out.println("\nPull Test:");
        System.out.println("Paint board current color:" + paintBoard.getCurrentColor());
        for (int i = 0; i < 3; i++) {
            pullTest(paintBoard);
        }
    }

    private static void pushTest(PaintBoard paintBoard) {
        System.out.printf("%s ---push---> ", paintBoard.getCurrentColor());
        paintBoard.push();
        System.out.println(paintBoard.getCurrentColor());
    }

    private static void pullTest(PaintBoard paintBoard) {
        System.out.printf("%s ---pull---> ", paintBoard.getCurrentColor());
        paintBoard.pull();
        System.out.println(paintBoard.getCurrentColor());
    }
}


Strategy(策略)模式
意图在于把可选的策略或方案封装在不同的类中,并在这些类中实现一个共同的操作。

Command(命令)模式
意图是把请求封装在对象中。

Interpreter(解析器)模式
意图是可以按照自己定义的组合规则集合来组合可执行对象。

你可能感兴趣的:(模式)