问题描述
我的代码包含以下读/写方法将数据写入数据库
My code contains the following read/write methods to write data to database
读取方法
- public byte [] readFile(arg1)
- public byte [] readFile(arg1,arg2)
- public InputStream readFile(arg1,arg2)
- public MyStream readFile(arg1,arg2)
- public byte [] readFile(arg1, arg2,arg3)
- public byte[]readFile(arg1)
- public byte[] readFile(arg1, arg2)
- public InputStream readFile(arg1, arg2)
- public MyStream readFile(arg1, arg2)
- public byte[] readFile(arg1, arg2, arg3)
编写方法
- public String [] writeFile(arg1)
- public String [] writeFile(arg1,arg2)
- public MyStream writeFile(arg1,arg2) - >文件是从这个输出的流写的。
- public String [] writeFile(arg1,arg2,arg3)
- public String[] writeFile(arg1)
- public String[] writeFile(arg1, arg2)
- public MyStream writeFile(arg1, arg2) -> file is written from this outputted stream
- public String[] writeFile(arg1, arg2, arg3)
各种类别访问这些方法用于读写目的。请以更好的方式为我提供更好的组织方式。
Various classed access these methods for read and write purpose. Kindly provide me a better way to organize this in a better way.
我希望有一个集中的部分,我的所有读/写都会发生。
I would like to have a centralized part where all my read/write would happen.
有人请帮助我选择一个合适的模式来组织这个。
Someone please help me to choosing an appropriate pattern to organize this well.
推荐答案
你可以这样做:
interface DatabaseServices {
... listing all the methods that you want to "combine"
class DatabaseServicesImpl implements DatabaseServices {
... implementing all those methods
enum DatabaseServicesProvider implements DatabaseServices {
INSTANCE;
private final DatabaseServices impl = new DatabaseServicesImpl(...
... forwarding all service calls to the impl object
上面的优点是它是完全单元可测试的(如果你只是创建一个枚举并在其上放置方法;你破坏了测试使用枚举的客户端的能力)。
The above has the advantage that it is "fully" unit-testable (if you just create an enum and put methods on it; you break your ability to test clients that are using the enum).
从那里,您进入现有的源代码,丢弃当前访问数据库的所有方法;用
From there, you go into your existing source code, and throw away all your methods that currently access your data base; replacing them with calls like
DatabaseServices services = DatabaseServicesProvider.INSTANCE;
...
services.whatever()
其中:枚举部分在某种程度上是可选的。但是你常常发现你想要这样的数据库相关服务是一个中央权威的事情(换句话说:单例)。枚举只是为那个全局方面提供了一个漂亮,干净的解决方案。如果你不需要一个中心实例(这实际上是一个更好的事情)方式),忘了那个部分。
Where: the "enum part" is somehow optional. But very often you find that you want such DB related services to be a central authority thing (in other words: a singleton). And enums simply provide a nice, clean solution to that "global" aspect. If you don't need a central instance (which would be actually a better thing anyway), just forget about that part.
这篇关于在我的项目中组织读/写操作的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!