存储/还原整个应用程序的快照/状态 [英] Store/Restore snapshot/state of entire application

查看:422
本文介绍了存储/还原整个应用程序的快照/状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想存储整个应用程序状态,然后在下次启动时将其还原.有没有一个图书馆对我来说会更容易?还是您有任何建议吗?

I want to store the entire application state and then restore it on the next start. Is there a library which would make it easier for me? Or does anyone of you have any suggestions?

独立应用程序

推荐答案

有一个设计模式可用于您的用途,它是Memento模式.

There is a design pattern that is used for your purpouse, and it is the Memento Pattern.

memento模式由三个对象实现:发起者,看守和memento.发起者是具有内部状态的某些对象.管理员将对发起者进行某些操作,但希望能够撤消更改.看守首先要向创建者索要纪念品.然后,它将执行将要执行的任何操作(或操作序列).要回滚到操作之前的状态,它将memento对象返回给发起者.纪念品对象本身是不透明的对象(看守不能更改或不应该更改的对象). -维基百科

The memento pattern is implemented with three objects: the originator, a caretaker and a memento. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation (or sequence of operations) it was going to do. To roll back to the state before the operations, it returns the memento object to the originator. The memento object itself is an opaque object (one which the caretaker cannot, or should not, change). - Wikipedia

您可以阅读Wiki页面中提供的示例,以了解如何在代码中使用它.

You can read the example provided in the wiki page to understand how to use it inside your code.

如果要将对象的状态另存为文件,并且即使在程序执行结束后仍可以使用,则应在要存储的类中实现Serializable接口.

If you want to save the state of an object as a file, and have it avaiable even after the execution of your program is over, you should implement the Serializable interface in the class you want to store.

示例:

public class Example implements Serializable
    {

    }

以及实例化该类的位置:

and where you instantiate that class:

try{

    Example c = new Example();
    FileOutputStream fout = new FileOutputStream("YOURPATH");
    ObjectOutputStream oos = new ObjectOutputStream(fout);   
    oos.writeObject(c);
    oos.close();
    System.out.println("Done");

   }catch(Exception ex){
       ex.printStackTrace();
   }

这篇关于存储/还原整个应用程序的快照/状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆