1. 程式人生 > >狀態模式【行為模式】

狀態模式【行為模式】

狀態模式 state abstract 特定 ole hang 實現 ret fin

狀態模式

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;
        }
    }

}

狀態模式【行為模式】