Skip to content

Commit

Permalink
Copy of strategy and state pattern before completely revising them.
Browse files Browse the repository at this point in the history
  • Loading branch information
budget-coder committed Jun 12, 2018
1 parent 88c1b1a commit e61ca6d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
10 changes: 9 additions & 1 deletion src/framework/StateDeviceStrategy.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package framework;

public interface StateDeviceStrategy extends DeviceStrategy {
public void setToPCOrDevice(boolean isPC);
//public void setToPCOrDevice(boolean isDevice);

public void setSrcAsMTPDevice(boolean option);

public void setDstAsMTPDevice(boolean option);

FileWrapper getSrcFileInstance(String path);

FileWrapper getDstFileInstance(String path);
}
48 changes: 38 additions & 10 deletions src/main/SwitchBetweenDevicesStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,71 @@
import framework.StateDeviceStrategy;

public class SwitchBetweenDevicesStrategy implements StateDeviceStrategy {
private DeviceStrategy mtpStrategy;
private DeviceStrategy pcStrategy;
private DeviceStrategy currentState;
private final DeviceStrategy mtpStrategy;
private final DeviceStrategy pcStrategy;
private DeviceStrategy currentSrcState;
private DeviceStrategy currentDstState;

public SwitchBetweenDevicesStrategy(DeviceStrategy mtpStrategy,
DeviceStrategy pcStrategy) {
public SwitchBetweenDevicesStrategy(final DeviceStrategy mtpStrategy,
final DeviceStrategy pcStrategy) {
this.mtpStrategy = mtpStrategy;
this.pcStrategy = pcStrategy;
}

@Override
public void setSrcAsMTPDevice(boolean isDevice) {
currentSrcState = (isDevice ? mtpStrategy : pcStrategy);
}

@Override
public void setDstAsMTPDevice(boolean isDevice) {
currentDstState = (isDevice ? mtpStrategy : pcStrategy);
}

/*
@Override
public void setToPCOrDevice(boolean isDevice) {
currentState = (isDevice ? mtpStrategy : pcStrategy); // Same as if-then-else
}
*/

@Override
public FileWrapper[] listDstFiles() {
return currentState.listDstFiles();
return currentDstState.listDstFiles();
}

@Override
public void copyMusicToDst(FileWrapper newMusic) throws IOException {
currentState.copyMusicToDst(newMusic);
// Iff. source and destination are on the PC, then use pcStrategy. Otherwise, use mtpStrategy.
if (currentSrcState.equals(currentDstState) && currentSrcState.equals(pcStrategy)) {
pcStrategy.copyMusicToDst(newMusic);
}
}

@Override
public boolean isDstADirectory() {
return currentState.isDstADirectory();
return currentDstState.isDstADirectory();
}

@Override
public FileWrapper getDstFolder() {
return currentState.getDstFolder();
return currentDstState.getDstFolder();
}

@Override
public FileWrapper getSrcFileInstance(String path) {
return currentSrcState.getFileInstance(path);
}

@Override
public FileWrapper getDstFileInstance(String path) {
return currentDstState.getFileInstance(path);
}

@Override
public FileWrapper getFileInstance(String path) {
return currentState.getFileInstance(path);
// TODO State pattern seems hard to make use of here other than when
// using the copy function.
throw new UnsupportedOperationException(getClass().getName() + ": getFileInstance not implemented");
}
}

0 comments on commit e61ca6d

Please sign in to comment.