这对于工厂模式来说是一个完美的问题吗?
我想以一定的灵活性设计应用程序的文件存储部分,这样就可以将文件存储在 S3 或 Web 服务器的硬盘上。
我还希望这对于每个用户来说都是灵活的,因此在他们的用户设置中,他们可以选择是否希望将文件存储在 S3 或服务器文件系统上。
我在想这样的事情:
IFileStorage fs = FileStorageFactory.Instance(userSettings);
然后我会有一个看起来像这样的方法:
public static IFileStorage Instance(UserSettings setting)
{
if(setting == UserSettings.S3)
return new S3FileStorage();
}
这有意义吗? (我是 ac# 程序员,但我将在 Java 中执行此操作)
我正在使用 Spring,但我认为此处不会使用 DI,因为实现会根据每个用户而变化。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
工厂和依赖注入并不相互排斥。 Spring有所谓的 工厂方法 生成 Bean。您可以将参数传递给工厂方法,它可以是静态的也可以是非静态的。
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.
是的。这似乎是合适的。您将类的特定实例返回给用户,因此只要他们的需求在程序中途没有改变,那么工厂就是合适的(否则我会推荐策略模式)
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)