我应该允许对象从其接口中删除自身吗? OOD 正确吗?
我有以下内容:
interface File
{
String name();
...
}
interface FileService
{
List<File> getAllFiles();
...
}
在使用此类接口时 如果我决定删除由 File 实例表示的实体(我的意思是从远程服务器删除),我应该怎么做?
我是否应该为文件接口引入 delete()
方法(因为它是信息专家并且可能知道如何从服务器删除自身)?或者我应该通过 void deleteFile(File file)
接口方法将此函数委托给它的创建者 - FileService
?为什么?
如果第一种情况更合适,如何使对象无效以避免其后续使用?
相关:我应该如何处理 uploadFile()
情况?谁应该为此负责?因为看起来FileService可能会违反SRP。
谢谢,
I have the following:
interface File
{
String name();
...
}
interface FileService
{
List<File> getAllFiles();
...
}
While working with the such interface If I decided to delete an entity represented by File instance (I mean to delete from a remote server), how should I do that?
Should I introduce delete()
method for File interface (since it is Information Expert and possibly knows how to delete itself from the server)? Or should I delegate this function to it's creator -- FileService
via void deleteFile(File file)
interface method? And why?
If the first case is more appropriate how to invalidate the object to avoid its following using?
And related: how should I handle uploadFile()
case? Who should be responsible for this? Because it seems that FileService will possibly violate SRP.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为它应该在
文件
上。如果您只有一个 FileService(每个与服务器的连接)充当文件工厂,您可能希望该服务保持最新。然后你可以使用一些东西:
编辑
现在有一个循环依赖,这不太好。 JVM 可能可以检测到它并清理任何内容,但您也可以使用 WeakReference。
无论您使用精简工厂和事实文件还是反之,这都是一种设计选择。但它们需要能够进行通信,因为通过工厂删除的文件应该知道它已被删除。
一些代码(假设事实工厂):
在这种情况下,我假设一个事实工厂,因为可能涉及网络连接,并且在多个对象和线程之间共享连接往往会使关闭该连接的责任不清楚。
因此,FactoryService 接口应该有一个方法
close()
或dispose()
来终止连接并使所有文件无效(因为它们不再可访问) )。编辑2
就OOD而言,我可能会尝试尽可能模仿java File API。因此,可以告诉对象删除自身。实现是在 File 中还是在 FileSystem 中的某个位置并不重要(对于接口和类的用户而言)。
I think it should be on
File
.If you have only one FileService (per connection to the server) which functions as the Factory of Files, you would probably want that service to keep up-to-date. You could then use something:
EDIT
There now is a cyclic dependency, which isn't great. The JVM can probably detect it and clean up any stuff, but you could also use a WeakReference.
It's a bit of a design choice whether you go with a thin factory and fact file, or the other way around. But they need to be able to communicate, as a file which is deleted through the factory should know it has been deleted.
Some code (assumes fact factory):
In this case I assume a fact factory as there is probably a network connection involved, and sharing a connection between multiple objects and threads tend to make the responsibility for closing that connection unclear.
This has as a consequence that the FactoryService interface should have a method
close()
ordispose()
which terminates the connection and invalidates all the File's (as they are not accessible anymore).EDIT 2
As far as OOD goes, I would probably try to mimic the java File API as far as possible. Thus, an object can be told to delete itself. Whether the implementation is in File or somewhere in FileSystem is not important (for the interface and the users of the class).
在我看来,这应该是FileService的责任。原因和假设是
假设 - 文件类型的任何对象都表示从某处检索的文件。这对消费者来说并不重要。我相信文件服务实现是对文件对象执行 CRUD 操作的实现。我还认为Fileservice更多的是文件管理方面的关注。因此删除应该在文件服务上。
如果文件对象中存在删除操作,那么我相信与文件相关的操作不具有凝聚力并且分散在各个类中。但这是我的看法。我很想听听其他意见!顺便说一句,java中的FILE类是静态的吗?
In my opinion, it should be the FileService's responsibility. The reason and assumptions are
Assumption - Any object of file type represents a file that is retrieve from somewhere. It doesnt matter to the consumer. Fileservice implementation i believe, is the one that performs CRUD operations upon the file object. I also believe that Fileservice is more of file management concern. Therefore delete should be on the file service.
If delete operation is present in the File object, then i believe file related operations are not cohesive and scattered across classes. Thats my opnion though. I would love to hear other opnions! BTW is the FILE class static in java?