This is something that I was wondering how to do, but could not find a way that suits the problem.
Suppose I have a class that could serve two purposes: Upload and Download. Instinctively, I would create an object somewhat like this:
public class File{
private FormFile uploadFile;
private File downloadFile;
private String uploadLocation;
private String downloadLocation;
//setters and getters
}
The thing is, I would want to have only one set of accessors for each type. For example, there is only one setFile(Object obj) in the class. But this would cause a problem because FormFile and File are two distinct objects.
I would then resort for OOP. Change my object to:
public interface File{
public void setFile(Object obj);
public Object getFile();
public void setLocation;
public String getLocation;
}
Then create the two other objects:
public UploadFile implements File{
private FormFile file;
private String location;
//implementation of methods
}
public Download implements File{
private File file;
private String location;
//implementation of methods
}
This could work if you clean other things within the code. But I realized that this would be harder to maintain afterwards. Is there another workaround to do this?
Something like:
File file = new File.UPLOAD;
would restrict the user to:
Use only method setFile(FileForm file){ }, and then setFile(File file){ } would become unavailable. Works vice versa when instantiating File file = new File.DOWNLOAD
I’m not really trying to solve anything on this. Just a thought.
Try with generics:
Usage: