mardi 29 juin 2021

Can the thread of an object be copied using prototype pattern?

I use the State Pattern to design the OrdinaryUserState class and DisableState for the User class. When a user becomes disabled, I start a timer "new timer (). Schedule ({...});", the user become normal after 24 hours. When I close the program, I save the user to the ".data" file. When I restart the program, I read out the user from the ".data" file. However, I find that the user is always disabled and cannot be changed even after 24 hours. The timer works when I don't close the program.

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

}

Aucun commentaire:

Enregistrer un commentaire