将具有静态属性的Singleton更改为Multiton

时间:2017-10-25 14:42:42

标签: java singleton multiton

我有以下Singleton类

public class TestFileEngine {
    private static int counter = 0;
    private static List<GeneratedFile> generatedFileList;
    private static Optional<TestFileEngine> engine = Optional.empty();

    private TestFileEngine() {
        generatedFileList = Collections.synchronizedList(new ArrayList<>());
    }

    public static synchronized TestFileEngine getInstance() {
        if (!engine.isPresent()) {
            engine = Optional.of(new TestFileEngine());
        }

        return ruEngine.get();
    }

    public synchronized void generateFile() {
        counter++;
        String timestamp = LocalDateTime.now().toString();
        String content = "";//insert random content generation
        GeneratedFile gf = new GeneratedFile(counter + ".txt", timestamp, content);
        generatedFileList.add(gf);
        System.out.println("Generated file " + counter + ".txt");
    }

    public GeneratedFile findFileByName(String filename) {
        for (GeneratedFile file : generatedFileList){
           if(file.getFileName().equals(filename)){
               return file;
           }
        }
        return null;
    }
}

现在我想拥有两个独立的引擎(未来可能还有更多),用于跟踪目的,我偶然发现了仍在使用惰性实现的多重模式。所以我将改变以下内容:

//Getting rid of Optional, as it can get funky with Maps
private static final Map<String, TestFileEngine> _engines = new HashMap<String, TestFileEngine>();

public static synchronized TestFileEngine getInstance(String key) {
    if (_engines.get(key) == null) {
        _engines.put(key, new TestFileEngine());
        System.out.println("Create engine " + key);
    }else {
        System.out.println("Using engine " + key);
    }

    return _engines.get(key);
}

我想确保每个引擎都有其独立的计数器和文件列表。但是,在我运行以下代码后,它们似乎共享了计数器和列表:

TestFileEngine.getInstance("http").generateFile();
TestFileEngine.getInstance("http").generateFile();
TestFileEngine.getInstance("http").generateFile();
TestFileEngine.getInstance("ftp").generateFile();
TestFileEngine.getInstance("ftp").generateFile();

System.out.println(TestFileEngine.getInstance("http").findFileByName("4.txt").getFileName());
System.out.println(TestFileEngine.getInstance("ftp").findFileByName("2.txt"));

控制台:

Create engine http
Generated file 1.txt
Using engine http
Generated file 2.txt
Using engine http
Generated file 3.txt
Create engine ftp
Generated file 4.txt
Using engine ftp
Generated file 5.txt
Using engine http
4.txt
Using engine ftp
null

我应该对countergeneratedFileList字段做些什么,以便从Multiton创建的每个TestFileEngine完全分开?

1 个答案:

答案 0 :(得分:2)

实际上,您使用static修饰符声明了该类的3个字段:

private static int counter = 0;
private static List<GeneratedFile> generatedFileList;
private static final Map<String, TestFileEngine> _engines = new HashMap<String, TestFileEngine>();

static个字段在当前类的所有实例之间共享 您想要engines字段。
但是,对于必须附加到counter的特定实例的generatedFileListTestFileEngine字段,您不希望这样。 因此,请将它们设为实例字段,而不是static

作为旁注,默认情况下,int字段的值为0,您应该避免_为您的变量添加前缀,这些变量不会成为命名约定的一部分。
所以你可以写:

private int counter;
private List<GeneratedFile> generatedFileList;
private static final Map<String, TestFileEngine> engines = new HashMap<String, TestFileEngine>();