状态模式【行为模式】

状态模式

Allow an object to alter its behavior when its internal state changes.
The Object will appear to change its class.
允许对象在其内部状态改变时改变其行为。
这个对象看起来会改变他的类型。
public class Status {
    /**
     * 状态模式:
     * Allow an object to alter its behavior when its internal state changes.
     * The Object will appear to change its class.
     * 允许对象在其内部状态改变时改变其行为。这个对象看起来会改变他的类型。
     */
    @Test
    public void all() {
        final StateMachine stateMachine = StateMachine.builder().build();
        stateMachine.open();
        stateMachine.close();
        stateMachine.close();
        stateMachine.runing();
        stateMachine.stop();
    }
}

/**
 * 1)状态机的所有有效状态,返回 true 表示允许切换
 */
interface IStatus {
    boolean open();

    boolean runing();

    boolean close();

    boolean stop();
}
/**
 * 2)抽象状态实现类
 */
abstract class AStaus implements IStatus {

    @Override
    public boolean open() {
        return false;
    }

    @Override
    public boolean runing() {
        return false;
    }

    @Override
    public boolean close() {
        return false;
    }

    @Override
    public boolean stop() {
        return false;
    }

}

/**
 * 3)具体状态实现:只能从一种状态切换到某些特定状态
 */
@Builder
@Slf4j
class OpenStatus extends AStaus {
    @Override
    public boolean close() {
        log.info("状态从 Open => Close");
        return true;
    }
}
/**
 * 3)具体状态实现:只能从一种状态切换到某些特定状态
 */
@Builder
@Slf4j
class CloseStatus extends AStaus {

    @Override
    public boolean open() {
        log.info("状态从 Close => Open");
        return true;
    }

    @Override
    public boolean runing() {
        log.info("状态从 Close => Runnig");
        return true;
    }

}
/**
 * 3)具体状态实现:只能从一种状态切换到某些特定状态
 */
@Builder
@Slf4j
class RunningStatus extends AStaus {
    @Override
    public boolean stop() {
        log.info("状态从 Running => Close");
        return true;
    }
}
/**
 * 3)具体状态实现:只能从一种状态切换到某些特定状态
 */
@Builder
@Slf4j
class StopStatus extends AStaus {
    @Override
    public boolean open() {
        log.info("状态从 Stop => Open");
        return true;
    }

    @Override
    public boolean runing() {
        log.info("状态从 Stop => Running");
        return true;
    }
}
/**
 * 4)状态机:内部维护着所有有效的状态
 */
@Builder
@Slf4j
class StateMachine implements IStatus {
    private static final IStatus OPEN = OpenStatus.builder().build();
    private static final IStatus CLOSE = CloseStatus.builder().build();
    private static final IStatus RUNNING = RunningStatus.builder().build();
    private static final IStatus STOP = StopStatus.builder().build();
    @Default
    private IStatus current = STOP;

    @Override
    public boolean open() {
        if (current.open()) {
            current = OPEN;
            return true;
        } else {
            log.info("无效的操作");
            return false;
        }
    }

    @Override
    public boolean runing() {
        if (current.runing()) {
            current = RUNNING;
            return true;
        } else {
            log.info("无效的操作");
            return false;
        }
    }

    @Override
    public boolean close() {
        if (current.close()) {
            current = CLOSE;
            return true;
        } else {
            log.info("无效的操作");
            return false;
        }
    }

    @Override
    public boolean stop() {
        if (current.stop()) {
            current = STOP;
            return true;
        } else {
            log.info("无效的操作");
            return false;
        }
    }

}
原文地址:https://www.cnblogs.com/zhuxudong/p/10164784.html