Re: Instanceof design doubt in Java
On 11/02/2011 8:08 AM, David wrote:
public class Transfer
{
Site origin;
Site destination;
TransferType typeOrigin;
TransferType typeDestination;
public void transferFile (String file)
{
typeOrigin.get (origin, file);
typeDestination.put (destination, file);
}
}
Does somebody knoes how avoid "instanceof" in TransferType classes? I
have thought in others designs but I haven't got a good solution.
I see the following classes in your design:
Site = abstract class or interface
^-- SharedFolderSite
^-- FTPSite
Site.get(String file);
Site.put(File localFile, String destinationFile);
class SharedFolderSite {
public File get(String file) {
// A file copy operation returning File which is where the
// local copy is.
}
public void put(File localFile, String destinationFile) {
// copy localFile destinationFile
}
}
class FTPSite {
public File get(String file) {
// FTP operation to a local file returning File which
// is where the local copy is.
}
public void put(File localFile, String destinationFile) {
// FTP put operation to destinationFile
}
}
Then transferFile becomes:
public void transferFile(String file)
{
File localFile = origin.get(file);
destination.put(localFile, file);
}
Walk me through why this wouldn't work?