对象的组合
我有一个班级充当经理并做一些工作。 当应用程序服务器启动时启动的 Servlet 会实例化该管理器。 我需要添加另一个类来完成其他工作,并且需要与经理协调。 我正在考虑将该类作为实例变量添加到管理器中。 我应该让管理器实例化新类(就像在构造函数中一样),还是让 servlet 实例化新类并在管理器实例化后调用 manager.setNewClass() ?
I have a class that acts as a manager and does some work.
A servlet that starts up when the application server starts up instantiates this manager.
I need to add another class that will do other work, and needs to coordinate with the manager.
I was thinking about adding the class to the manager as an instance variable.
Should I have the manager instantiate the new class (like in the constructor), or have the servlet instantiate the new class and call manager.setNewClass() after the manager has been instantiated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,作为一个粗略的概括,您应该在 servlet 中实例化它并将其传递给管理器(通过构造函数参数,或通过 setNewClass())...而不是注入依赖项而不是对它们进行硬编码。
但是,根据您的具体用例,即使这也可能不是正确的答案。您可能最好使用 Builder 来构造管理器类。这样,构建器就可以管理整个管理器(包括任何依赖项)的构建,而不是将其硬编码到 servlet 中。这会将依赖项从 servlet 移出并移入构建器(您可以在测试和其他代码中更好地处理它)。
简短的回答是没有灵丹妙药。如果不了解所有类之间的硬关系以及角色和职责,就很难说出最好的方法。但是在构造函数中实例化几乎从来都不是一个好主意,您应该以某种形式注入依赖项(但从哪里注入还有待争论)...
Well, as a gross-over-generalization, you should instantiate it in the servlet and pass it into the manager (either via a constructor parameter, or via
setNewClass()
)... Inject the dependencies rather than hard-code them.However, depending on your exact use-case, even that might not be the right answer. You might be better off with a Builder for constructing the manager class. That way, the builder manages the construction of the entire manager (including any dependencies) rather than hard-coding it into the servlet. This would move the dependency out of the servlet and into the builder (which you can better deal with in tests and other code).
The short answer is that there's no silver bullet. Without knowing the hard relationships between all of the classes, and the roles and responsibilities, it's hard to say the best method. But instantiating in a constructor is almost never a good idea and you should inject the dependency in some form or another (but from where is up for debate)...
这让我想起了 FFF 模式。
在哪里创建实例并不重要。只需在最适合您的地方进行创建,如果您在其他地方需要它,只需应用一些基本的重构即可。
如果您确实需要解耦,请尝试使用 Guice 等工具,但前提是您确实需要它。
This reminds me of the FFF pattern.
It does not matter where you create the instance. Just create wherever it fits you best, and if you need it somewhere else, just apply some basic refactoring.
If you really need decoupling try using some tool like Guice, but only if you really need it.
您应该选择后者——它将管理器与其委托分离。为了正确地进行解耦,您应该创建一个定义管理器期望的行为的接口,然后通过控制反转/依赖注入提供实现。这将允许您单独测试管理器及其工作人员类(我将其称为委托,但可能不是)。
编辑——这个答案假设java,因为你提到了servlet。
你有你的经理类,在其中你期望一个接口
Worker 是一个接口。它定义了行为,但没有定义实现,
您现在需要创建一个实现。
经理只知道它会获得一些工作人员。您可以提供实现该接口的任何类。这是解耦——管理器不绑定到任何特定的实现,它只绑定到工作器的行为。
You should do the latter -- it decouples the manager from its delegate. To do the decoupling correctly, you should create an interface that defines the behavior the manager expects, and then provide an implementation via inversion of control/dependency injection. This will allow you to test the manager and its worker class (I called it a delegate, but it might not be) in isolation.
EDIT -- this answer assumes java because you mentioned servlet.
You have your manager class, in it you expect an interface
Worker is an interface. It defines behavour but not implementation
you now need to create an implementation
The manager just knows it gets some Worker. You can provide any class that implements the interface. This is decoupling -- the Manager is not bound to any particular implementation, it is bound only to the behavour of a worker.