标签:否则 包含 增强 failure text bubuko test fail 个数
状态模式允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它所属的类,属于行为型模式。
优点:
缺点:
定义抽象状态角色ConnectState
public interface ConnectState {
void handleAction();
}
定义具体状态角色ReconnectState
public class ReconnectState implements ConnectState {
@Override
public void handleAction() {
// 重连逻辑
}
}
定义具体状态角色SuccessState
public class SuccessState implements ConnectState {
@Override
public void handleAction() {
// 成功逻辑
}
}
定义具体状态角色FailureState
public class FailureState implements ConnectState {
@Override
public void handleAction() {
// 失败逻辑
}
}
定义环境角色Context
public class Context {
private ReconnectState reconnectState;
private FailureState failureState;
private SuccessState successState;
public void reconnect() {
if (reconnectState == null) {
reconnectState = new ReconnectState();
}
reconnectState.handleAction();
}
public void failure() {
if (failureState == null) {
failureState = new FailureState();
}
failureState.handleAction();
}
public void success() {
if (successState == null) {
successState = new SuccessState();
}
successState.handleAction();
}
}
标签:否则 包含 增强 failure text bubuko test fail 个数
原文地址:https://www.cnblogs.com/fomin/p/10043330.html