赞
踩
- public void push(ColorState state){
- switch (state) {
- case RED:
- state=ColorState.GREEN;
- break;
- case GREEN:
- state=ColorState.BLUE;
- case BLUE:
- state=ColorState.RED;
- default:
- break;
- }
- }
- public void push(ColorState state){
- switch (state) {
- case RED:
- state=ColorState.GREEN;
- break;
- case GREEN:
- state=ColorState.BLUE;
- case BLUE:
- state=ColorState.RED;
- default:
- break;
- }
- }
- public interface State {
- String getColor();
-
- void push(PaintBoard paintBoard);
-
- void pull(PaintBoard paintBoard);
- }
- public class RedState implements State{
-
- @Override
- public String getColor() {
- return "RED";
- }
-
- @Override
- public void push(PaintBoard paintBoard) {
- paintBoard.setState(new GreenState());
- }
-
- @Override
- public void pull(PaintBoard paintBoard) {
- paintBoard.setState(new BuleState());
- }
-
- }
- public class BuleState implements State {
-
- @Override
- public String getColor() {
- return "BULE";
- }
-
- @Override
- public void push(PaintBoard paintBoard) {
- paintBoard.setState(new RedState());
- }
-
- @Override
- public void pull(PaintBoard paintBoard) {
- paintBoard.setState(new GreenState());
- }
-
- }
- public class GreenState implements State {
-
- @Override
- public String getColor() {
- return "GREEN";
- }
-
- @Override
- public void push(PaintBoard paintBoard) {
- paintBoard.setState(new BuleState());
- }
-
- @Override
- public void pull(PaintBoard paintBoard) {
- paintBoard.setState(new RedState());
- }
-
- }
- public class StateTest {
- public static void main(String[] args) {
- PaintBoard paintBoard=new PaintBoard(new RedState());
- System.out.println(paintBoard.getColor());
- paintBoard.push();
- System.out.println(paintBoard.getColor());
- paintBoard.push();
- System.out.println(paintBoard.getColor());
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。