如何在整个应用程序中保存文件集合?

| 在我的移动应用程序中,我需要保存一个文件对象(图片,文档)的集合,可以在整个应用程序中进行访问,并且用户可以对该集合进行各种操作: 查看全部/个人文件 将集合的子集上载到服务器 共享单个文件 ... 该集合仅初始化一次。 我想知道将单例模式用作保存集合的对象是一个好主意,这样我就不必在用户每次打开特定屏幕时都初始化集合吗?     
已邀请:
绝对是单例模式的目的。 从维基百科,单例模式是   当只有一个对象时有用   需要协调行动   系统。 例:
public class SingletonCollection {

    private Collection<File> fileCollection;
    private static SingletonCollection instance;

    private SingletonCollection() {
        fileCollection = new ArrayList<File>();
    }

    public static SingletonCollection getInstance() {
        if (instance == null) {
            instance = new SingletonCollection();
        }

        reutrn instance;
    }

    public void addFile(File f) {
        fileCollection.add(f);
    }

    public Collection<File> getFiles() {
        return fileCollection;
    }
}
    
对于Java> = 1.5
public enum FileCollector
{
    INSTANCE;
    private FileCollector()
    {
        List _temp = new ArrayList();
        File f = new File(\"properties\");
        _temp.add(f);
         fileContainer = Collections.unmodifiableList(_temp);
    }

   private final Collection<File> fileContainer;

   public Collection<File> getFiles() {
        return fileContainer;
    }

}
    
如果collection仅初始化一次,则使用singleton。毫无疑问。     
如果您使用的是Java ME,请尝试使用RecordStore。您可以从应用程序中的任何位置访问它。     

要回复问题请先登录注册