Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce memory foot print by replace anonymous class with singleton. But need to refactor the design more

So I have this method that get executed repeatedly

public static boolean isReady(String dirPath, int numPdfInPrintJob){
    File dir = new File(dirPath);
    String[] fileList = dir.list(new FilenameFilter(){
        public boolean accept(File file, String filename) {
            return (filename.toLowerCase().endsWith(".pdf"));
        }
    });
    if(fileList.length >= numPdfInPrintJob) return true;
    else return false;  
}

This method using anonymous class that will create a new instance of FilenameFilter every time invoked and I invoke this method a lot. So I want to make this anonymous class into a singleton. So my initial thought is to create a new singleton class that look like this

public class PdfFileNameFilter implements FilenameFilter{
    private PdfFileNameFilter(){} //non-instantible

    //guarantee to only have one instance at all time
    public static final PdfFileNameFilter INSTANCE = new PdfFileNameFilter();

    public boolean accept(File dir, String name) {
        return (name.toLowerCase().endsWith(".pdf"));
    }
}

Can I refactor this a bit more. I need to do ZipFileNameFilter as well, and maybe many different file extension filter. Dont want to create a class for each filter. I need to refactor this design a bit more. Maybe interface come into place somewhere here.

like image 353
Thang Pham Avatar asked Jun 03 '11 14:06

Thang Pham


People also ask

Why do we need Singleton class?

The primary purpose of a Singleton class is to restrict the limit of the number of object creation to only one. This often ensures that there is access control to resources, for example, socket or database connection.

How do I create a multiple instance of a Singleton class?

Singleton patten means only one instance is allowed. So there is no question of creating multiple instances. Though there are some hacks and workarounds like Serializing the Object and De Serializing it back or using different Class loaders but again it violates the basic principle why Singleton pattern is created for.

How do you destroy a Singleton object in Java?

There is no way to destroy a Singleton without breaking the Singleton property. As others have said, maybe a Singleton isn't appropriate here. If the number of instances of the class can go down to zero, then it's not a Singleton.

What is the use of Singleton class in Java?

The Singleton's purpose is to control object creation, limiting the number of objects to only one. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields.


2 Answers

If all you wanted to do was reduce memory usage you could have done

private static final FilenameFilter PDF_FILES = new FilenameFilter(){
    public boolean accept(File file, String filename) {
        return (filename.toLowerCase().endsWith(".pdf"));
    }
}

If you want to create a singleton, the simplest way is

public enum PdfFileNameFilter implements FilenameFilter {
    INSTANCE;

    public boolean accept(File dir, String name) {
        return (name.toLowerCase().endsWith(".pdf"));
    }
}
like image 90
Peter Lawrey Avatar answered Sep 24 '22 15:09

Peter Lawrey


It seems simpler to me to just use your existing anonymous class and make one instance that all your method invocations use.

private static final FilenameFilter PDF_FILTER = new FilenameFilter() {
    public boolean accept(File file, String filename) {
        return (filename.toLowerCase().endsWith(".pdf"));
    }
}

public static boolean isReady(String dirPath, int numPdfInPrintJob){
    File dir = new File(dirPath);
    String[] fileList = dir.list(pdfFilter);
    if(fileList.length >= numPdfInPrintJob) return true;
    else return false;  
}

This is a case where subclassing and making a singleton seems to be a tad overkill: you simply want only one instance to use right here, whereas a singleton is used when there is only one instance you will ever want to use.

like image 44
Cajunluke Avatar answered Sep 26 '22 15:09

Cajunluke