bumperbox
bumperbox

Reputation: 10214

Interface or Abstract class

My Java is a bit rusty, and I am sure I am doing this wrong

I have a whole bunch of different fileformats that I need to export. So I though I would create a class for each format, and have them all implement a standard Interface.

Each document has a name, and default filename, but I am not sure how I should store that data correctly. Initially I thought it should be a static final string in the concrete class, but then how do I enforce it via the interface so that all concrete classes have to supply that info.

Hope this makes sense, this is what I have so far

public interface IExporter {

    public void exportToFile(String filename) throws SQLException, IOException;

    public String getDocumentName();

    public String getDefaultFilename();

}

-

public class LaneDrawCsvExporter implements IExporter {

    public void exportToFile(String filename) throws SQLException, IOException {
         // code to export document here
    }

    public String getDocumentName() {
        return "Lane Draw";
    }

    public String getDefaultFilename() {
        return "Lane Draw.CSV";
    }
}

Upvotes: 1

Views: 134

Answers (4)

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26737

If you need to force to set up the initial state of your class(filename etc) it would be better to use an Abstract class. The below is just to give you an idea(you need to refactor it)

NOTE: As there is no default (or no-arg) constructor in the parent abstract class the constructor used in subclasses must be specified.

public abstract class Exporter {

    string FileName
    public Exporter ( string fileName ) {
        FileName= fileName 
    }

    public void exportToFile() ....

    public String getDocumentName()....

    public String getDefaultFilename()....

}
-

public class LaneDrawCsvExporter extends Exporter {

 public LaneDrawCsvExporter ( string fileName ) {
        super(fileName) 
    }

public void exportToFile() throws SQLException, IOException {
 // code to export document here
}

public String getDocumentName() {
    return "Lane Draw";
}

public String getDefaultFilename() {
    return "Lane Draw.CSV";
}
}

Upvotes: 0

Mechkov
Mechkov

Reputation: 4324

I would create an abstract class called BaseIEExporter or something and then provide a couple (or more) instance variables and their getter and setter methods within (like name/filename). Or the common characteristics between all implementing classes.

When you create a new class extending the abstract class, just have the overloaded constructor provide whatever parameters you need supplied.

This way the process of creation of a certain class, would require you to provide the needed information i.e the constructor requires it.

Upvotes: 1

Mark Rotteveel
Mark Rotteveel

Reputation: 108994

Separate the information on the files from the Exporters. So you have a bunch of Exporters whose only tasks is to export the file, and some object that links to the file and the right exporter (and maybe some addition constructs to decide which Exporter to use).

Upvotes: 1

QuantumMechanic
QuantumMechanic

Reputation: 13946

Your approach looks reasonable. As for how to enforce having to supply doc name and default filename, I think what you've done works -- having methods in the interface that return the doc name and the default filename. All concrete classes implementing the interface will have to implement those methods to even be able to compile. Thus you've enforced the requirement that the concrete classes have to supply those names. I think trying to enforce that concrete classes have a particularly-named static final string is a red herring since you have those methods available.

Upvotes: 2

Related Questions