public class DataConverter { public static final String DATA_FILE = "Data.txt"; public static final String OUTPUT_FILE = "SQL.txt"; PRivate long ID = 0; public Memento createMemento() { return (new Memento(ID)); } public void setMemento(Memento memento) { if (memento != null) ID = memento.getID(); } public long getLastProcessedID() { return ID; } public void setLastProcessedID(long lastID) { ID = lastID; } public boolean process() { boolean sUCcess = true; String inputLine = ""; long currID = 0; try { File inFile = new File(DATA_FILE); BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(inFile))); long lastID = getLastProcessedID(); while ((inputLine = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(inputLine, ","); String strID = st.nextToken(); currID = new Long(strID).longValue(); if (lastID < currID) { Customer c = new Customer(strID, st.nextToken(), st.nextToken(), st.nextToken()); if (!(c.isValid())) { success = false; break; } ID = new Long(strID).longValue(); FileUtil util = new FileUtil(); util.writeToFile(OUTPUT_FILE, c.getSQL(), true, true); } } br.close(); }//Try catch (Exception ex) { System.out.println(" An error has occurred " + ex.getMessage()); System.exit(1); } if (success == false) { System.out.println("An error has occurred at ID=" + currID); System.out.println("Data Record=" + inputLine); return false; } return true; } class Memento implements java.io.Serializable { private long lastProcessedID; private Memento(long ID) { lastProcessedID = ID; } private long getID() { return lastProcessedID; } }//end of class }//end of class
public class DCClient { public static void main(String[] args) { MementoHandler objMementoHandler = new MementoHandler(); DataConverter objConverter = new DataConverter(); objConverter.setMemento(objMementoHandler.getMemento()); if (!(objConverter.process())) { System.out.println("Description: Invalid data - " + "Process Stopped"); System.out.println("Please correct the Data and " + "Run the application Again"); objMementoHandler.setMemento( objConverter.createMemento()); } } }
public class WindowsSystem { private String state; public Memento createMemento() { //创建备份,保存当前状态 return new Memento(state); } public void restoreMemento(Memento memento){ //从备份中恢复系统 this.state=memento.getState(); } public String getState(){ //获得状态 return this.state; } public void setState(String state){ //设置状态 this.state=state; System.out.println(当前系统处于+this.state); } }
2、再定义备份(Memento)类:
public class Memento { private String state; public Memento(String state) { //备份 this.state=state; } public String getState(){ //获得状态 return this.state; } public void setState(String state){ //设置状态 this.state=state; } }
3、定义用户(User)类:
public class User { private Memento memento; public Memento retrieveMemento() { //恢复系统 return this.memento; } public void saveMemento(Memento memento){ //保存系统 this.memento=memento; } }
4、编写测试类:
public class Test { public static void main(String args[]) { WindowsSystem WinXP = new WindowsSystem(); //Winxp系统 User user = new User(); //某一用户 Winxp.setState(好的状态); //Winxp处于好的运行状态 user.saveMemento(Winxp.createMemento()); //用户对系统进行备份,Winxp系统要产生备份文件 Winxp.setState(坏的状态); //Winxp处于不好的运行状态 Winxp.restoreMemento(user.retrieveMemento()); //用户发恢复命令,系统进行恢复 System.out.println(当前系统处于+Winxp.getState()); } }