views:

77

answers:

2

I want to design the file storage part of my application with some flexibility, so one can store files in either S3 or on the web server's hard drive.

I also want this to be flexible on a per user basis, so in their user settings they can choose if they want their files stored in S3 or on the servers file system.

I am thinking of something like this:

IFileStorage fs = FileStorageFactory.Instance(userSettings);

Then I would have a method that looks like:

public static IFileStorage Instance(UserSettings setting)
{
     if(setting == UserSettings.S3)
         return new S3FileStorage();
}

Does this make sense? (I'm a c# programmer but I will be doing this in Java)

I am using Spring, but I don't think DI will be used here since the implementation changes on a per user basis.

+1  A: 

Yes. That seems appropriate. You're returning a particular instance of a class to the user, so as long as their requirements don't change halfway through the program, then the factory is appropriate (otherwise I'd recommend a strategy pattern)

Brian Agnew
good point, the strategy is a little cleaner I think.
Blankman
The strategy provides a *dynamic* behavioural change. I'm not sure if you need that capability ?
Brian Agnew
+2  A: 

Factory and Dependency Injection aren't mutually exclusive. Spring has the so-called factory-method which produces beans. You can pass arguments to the factory-method, and it can be either static or non-static.

Bozho