2

I am working on a web app project which requires me to refactor file uploading feature. Our file uploading feature supports multiple sources of uploading file, such git repo, nexus URL, zip files and we also want to support more such as perforce in the future. In the current implementation, the application has multiple endpoints to handle different methods such as getNexusFile(), getGitFile(), getZipFile(); under each method, there is implementation for retrieving files from specified source.

My idea is to merge all these methods into one method called getUploadFile() by using Strategy Pattern. As in the Strategy pattern, algorithms could be selected at runtime, so files uploaded from different sources could be treated indifferently after I specify the right strategy. For every source, I’ll create a strategy for it.

My question is: is that a good practice of design pattern? Is there any better approach to model this question?

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Larry Li
  • 23
  • 5

1 Answers1

3

Strategy pattern is a valid approach for this problem.

Class SomeClass {
  private FileUploader fileUploader;
}

Interface FileUploader {
     public void uploadFile();
}

Class GitFileUploader implements FileUploader{
     public void uploadFile() {//Implementation for Git File Upload}
}

Class NexusFileUploader implements FileUploader {
     public void uploadFile() { //Implementation for Nexus }
}

You can add more implementations of FileUploader as needed.

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Amit G.
  • 323
  • 3
  • 8
  • This only makes sense if the concrete `FileUploader` is not modified during the lifetime of the `SomeClass` object. If a single `SomeClass` object is expected to upload different files from different sources, using a *Factory Method* or *Abstract Factory* might be more appropriate. – Frank Puffer Feb 25 '17 at 10:15