51,411
社区成员
发帖
与我相关
我的任务
分享我用状态模式为User类设计了正常使用态和禁用状态,当用户变为禁用态后,开启一个定时器new Timer().schedule({......});,24小时后恢复为正常态。当关闭程序时,把User类利用深复制保存到.data文件,重启程序后从.data文件读出User类,但我发现User类一直处于禁用状态,即使过了24小时也不能改变。当我不关闭程序时定时器能够正常使用。
public class User implements Serializable {
private AbstractState state;
private String name;
private String password;
private Password generator;
private Unique uniguepassword;
public boolean isVIP() {
return isVIP;
}
public Unique getUniguepassword() {
return uniguepassword;
}
public void setUniguepassword(Unique uniguepassword) {
this.uniguepassword = uniguepassword;
}
public void setVIP(boolean VIP) {
isVIP = VIP;
}
private boolean isVIP;
public User(String name,String password){
this.name=name;
this.password=password;
this.state=new OrdinaryUserState(this); //初始状态
this.generator=new Password();
this.uniguepassword=new UniquePassword();
}
public void generate(int chance){
state.generatePassword(chance);
}
public void registerVIP(){
state.registerVIP();
}
public void restore(){
state.restore();
}
public AbstractState getState() {
return state;
}
public void setState(AbstractState state) {
this.state = state;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Password getGenerator() {
return generator;
}
public void setGenerator(Password generator) {
this.generator = generator;
}
}
public abstract class AbstractState implements Serializable {
protected User user;
protected int chances;
protected String stateName;
protected Date date;
public abstract void checkState(int chances);
public void generatePassword(int chance) {
this.chances -= chance;
checkState(chances);
}
public void restore(){
this.chances=5; //恢复正常用户状态
checkState(chances);
}
public void registerVIP(){
user.setVIP(true);
this.chances=10000;
checkState(chances);
}
public int getChances() {
return chances;
}
public void setChances(int chances) {
this.chances = chances;
}
public String getStateName() {
return stateName;
}
public void setStateName(String stateName) {
this.stateName = stateName;
}
}
public class OrdinaryUserState extends AbstractState implements Serializable {
public OrdinaryUserState(User user){
this.chances=5;
this.user=user;
this.stateName="普通用户";
}
public OrdinaryUserState(AbstractState state) {
this.user=state.user;
this.chances=state.chances;
this.stateName="普通用户";
}
@Override
public void checkState(int chances) {
if(chances==0){
user.setState(new DisableState(this));
}
if(chances>5){
user.setState(new VIPUserState(this));
}
}
}
public class DisableState extends AbstractState implements Serializable {
public DisableState(AbstractState state) {
this.user=state.user;
this.chances=state.chances;
this.stateName="禁用状态";
//this.date=new Date();
//System.out.println(this.date.getTime());
refresh();
}
public void refresh(){
new Timer().schedule(new TimerTask() {
@Override
public void run() {
restore();
}
},24*60*60*1000,24*60*60*1000);
}
@Override
public void checkState(int chances) {
if(chances==5){
user.setState(new OrdinaryUserState(this));
}
if(chances>5){
user.setState(new VIPUserState(this));
}
}
}