如何编写一个强大的、可定制的应用程序来对文件执行任务?
我想编写一个程序,该程序将在给定的输入目录中递归运行,并对其遇到的内容执行预定义的任务。 我希望它足够强大,这样我就可以开发应用程序,并且如果我想在将来添加其他行为,则不必进入核心代码。
在我看来,要执行的每个任务都应该在一个类中实现,并且应该有一个配置文件,例如将类映射到文件类型。
我对如何实现这一点有一些想法,但无法将其完全形成解决方案。
这应该通过依赖注入来完成吗?如果是这样,哪些简单的 DI 框架适合这个?
或者也许应该只通过读取配置文件并加载其中定义的类来完成?
I want to write a program that will run recursively through a given input directory and will execute pre-defined tasks on what it encounters into.
I want it to be robust, so that I can develop the app and don't have to go into the core code if I want to add another behavior in the future.
The way I see it, each task to perform should be implemented in a class, and there should be a configuration file mapping classes to file types, for example.
I have some idea of how to implement this but can't quite form it into a solution.
Should this be done with dependency injection? And if so, which simple DI frameworks suit for this?
Or maybe it should be done just with reading the configuration file and loading classes that are defined there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您需要的复杂程度,您可以查看 http://static.springsource.org/ spring-batch/
如果您需要一种更简单的方法,您可以公开一个由所有处理类实现的“类似命令”的接口,以及某种工厂类,它将根据您的映射实例化这些实现。< br>
这样,您将主要通过调用每个文件的工厂,然后调用“process”方法来使用接口。
添加新任务应该像创建接口的新实现并将其添加到映射文件一样简单。
干杯
Depending on how complex you need it, you might take a look at http://static.springsource.org/spring-batch/
If you need a simpler approach you could expose a "command-like" interface to be implemented by all your processing classes, and some kind of factory class that will instantiate these implementations based on your mapping.
This way you'll be working mainly with the interface by calling the factory for each file, and then invoking the "process" method.
Adding new tasks should be as easy as creating a new implementation of the interface and adding it to the mapping file.
Cheers