使用依赖注入还是有更简单的解决方案?
在我们的项目中,我们有一个类 KnowledgeBaseManager,它被其他类使用,如下所示:
KnowledgeBaseManager manager = KnowledgeBaseManager.get();
manager.foo();
KnowledgeBaseManager 持有一个静态变量 standardKnowledgeBaseManager,它在第一次使用时被初始化:
class KnowledgeBaseManager {
private static KnowledgeBaseManager standardKnowledgeBaseManager = null;
public static KnowledgeBaseManager get() {
if (standardKnowledgeBaseManager == null) {
standardKnowledgeBaseManager = new KnowledgeBaseManager();
// initialize standardKnowledgeBaseManager with appropriate knowledge base
}
return standardKnowledgeBase;
}
此外,我们有一个参数化构造函数
public static KnowledgeBaseManager get(OntModel model) {...}
,到目前为止我们只使用它来表示单元test,我们需要一个在后台有测试知识库的KnowledgeBaseManager。
现在,我们面临以下挑战:对于开发,我们希望应用程序使用知识库管理器,并在后台使用另一个知识库(因为速度)。更具体地说,我们使用 Wicket 构建一个 Web 应用程序。因此,我们想在应用程序开始的某个位置声明应用程序中使用的知识库和 KnowledgeBaseManager(取决于我们是在开发还是部署)。使用 KB 管理器的代码(像
KnowledgeBaseManager manager = KnowledgeBaseManager.get();
现在一样)不应为此进行更改。
问题是:最好的架构是什么?
我正在考虑使用像 PicoContainer 或 Guice 这样的依赖注入框架,但没有任何使用经验,并且我不确定这是否会成为该特定问题的开销。对于我们案例的最佳实践有什么建议吗?
In our project, we have a class KnowledgeBaseManager, which gets used by other classes as follows:
KnowledgeBaseManager manager = KnowledgeBaseManager.get();
manager.foo();
KnowledgeBaseManager holds a static variable standardKnowledgeBaseManager which gets initialized when used for the first time:
class KnowledgeBaseManager {
private static KnowledgeBaseManager standardKnowledgeBaseManager = null;
public static KnowledgeBaseManager get() {
if (standardKnowledgeBaseManager == null) {
standardKnowledgeBaseManager = new KnowledgeBaseManager();
// initialize standardKnowledgeBaseManager with appropriate knowledge base
}
return standardKnowledgeBase;
}
Furthermore, we have a parametrized constructor
public static KnowledgeBaseManager get(OntModel model) {...}
which we use until now just for unit test, where we need a KnowledgeBaseManager with a test knowledge base in the background.
Now, we have the following challenge: For development, we want that the application uses a KB-manager with another knowledge base in the background (because of speed). To be more specific, we build a web application with Wicket. So we want to declare somewhere at the start of the app, which knowledge base and KnowledgeBaseManager is used in the application (depending on wheter we are in development or deployment). The code for using the KB-manager (like
KnowledgeBaseManager manager = KnowledgeBaseManager.get();
now) should not be changed for that.
The question is: what's the best architecture for that?
I was thinking on using a dependency injection framework like PicoContainer or Guice, but don't have any experience with it and I am not sure if that would be overhead for that particular problem. Any suggestions for best practices our case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在实现单例模式 - 如果您的代码是多线程的,那么您的实现是错误的(请参阅 http://java.sun.com/developer/technicalArticles/Programming/singletons/ )
此外,正如您所发现的,单例(即全局变量)是不利于测试。依赖注入是答案之一。首先忽略所有框架。这意味着您以传统方式编写 KnowledgeBaseManager - 具有构造函数和方法的类,没有静态单例因子方法。使用 KnowledgeBaseManager 的代码不会实例化或查找 KnowledgeBaseManager - 它而是通过构造函数或 setter 方法接收它(我更喜欢前者):
请注意,鉴于该类不会查找/实例化管理器,它会不在乎您是否使用单例或其他什么,并且您可以在测试中轻松实例化不同的管理器,而无需以任何方式接触您的代码。
以这种方式构建代码后,您可能会发现最终会得到一些丑陋的大类,它们只是实例化您需要的所有对象,您需要一些额外的代码将对象绑定到不同的范围(即会话或请求范围的对象),并且使用配置文件来控制实例化...如果您发现自己正在编写大量重复或样板代码,您可以查看 DI 框架,这可能会帮助您避免这种情况。
但对于很多程序,您也许能够在不使用 DI 框架的情况下编写 DI 风格的代码。
You are implementing the singleton pattern- and if your code is multithreaded, you are implementing it wrong (see http://java.sun.com/developer/technicalArticles/Programming/singletons/ )
Moreover, as you have discovered, singletons (i.e. global variables) are bad for testing. Dependency injection is one of the answers. Ignore all frameworks first. It means you write your KnowledgeBaseManager in the traditional way- a class with constructor and methods, no static singletonish-factorish methods. Code using KnowledgeBaseManager does not instantiate or lookup the KnowledgeBaseManager- it rather receives it via constructor or setter method (I prefer the former):
Note that given that the class does not lookup/instantiate the manager, it does not care whether you are using a singleton or what, and that you can easily instantiate a different manager in your test without touching your code in any way.
After you structure your code in this manner, you might find that you end up with big ugly classes that just instantiate all objects you need, that you need some extra code to tie objects to different scopes (i.e. session or request-scoped objects) and to use config files to control the instantiation... if you find that you are writing lots of repetitive or boilerplate code, you can check out the DI-frameworks, which will probably save you from that.
But for a lot of programs, you might be able to write DI-style code without using a DI-framework.
您可以使用构造设计模式。
您可以创建一个工厂并配置它以返回您需要的对象。
You could use a constructional design pattern.
You could create a factory and configure it to return the objects you need.