有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java可以使用原型模式复制对象的线程吗?

我使用状态模式为用户类设计OrdinaryUserState类和DisableState。当用户被禁用时,我启动一个计时器“newtimer().Schedule({…});”,用户在24小时后恢复正常。当我关闭程序时,我将用户保存到“.data”文件中。当我重新启动程序时,我从“.data”文件中读取用户。但是,我发现该用户始终处于禁用状态,即使在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); //init
    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;  //become normal
    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="OrdinaryUser";
}
public OrdinaryUserState(AbstractState state) {
    this.user=state.user;
    this.chances=state.chances;
    this.stateName="OrdinaryUser";
}
@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="disabled";
    //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));
    }
}

}

public class saveUser {   //save the user
public saveUser(){};
public void save(User user) throws IOException, ClassNotFoundException {
    String path = "D:/aaa/.data";
    FileOutputStream fileOutputStream;
    ObjectOutputStream objectOutputStream;
    File file = new File(path);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();
    }
    if (file.exists()) {
        file.delete();
    }
    file.createNewFile();

    fileOutputStream = new FileOutputStream(file.toString());
    objectOutputStream = new ObjectOutputStream(fileOutputStream);
    objectOutputStream.writeObject(user);
    objectOutputStream.close();
    fileOutputStream.close();
}

public User copy() throws IOException, ClassNotFoundException {
    String path = "D:/aaa/.data";
    User user;
    FileOutputStream fileOutputStream;
    ObjectOutputStream objectOutputStream;
    File file = new File(path);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();
    }
    if (file.exists()) {
        FileInputStream fileInputStream;
        ObjectInputStream objectInputStream;
        fileInputStream = new FileInputStream(file.toString());
        objectInputStream = new ObjectInputStream(fileInputStream);
        user = (User) objectInputStream.readObject();
        objectInputStream.close();
        fileInputStream.close();
        return user;
    } else {
        return null;
    }
}

}


共 (0) 个答案