对象的组合

发布于 2024-10-15 06:00:11 字数 187 浏览 2 评论 0原文

我有一个班级充当经理并做一些工作。 当应用程序服务器启动时启动的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桃扇骨 2024-10-22 06:00:11

好吧,作为一个粗略的概括,您应该在 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)...

长亭外,古道边 2024-10-22 06:00:11

这让我想起了 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.

不语却知心 2024-10-22 06:00:11

您应该选择后者——它将管理器与其委托分离。为了正确地进行解耦,您应该创建一个定义管理器期望的行为的接口,然后通过控制反转/依赖注入提供实现。这将允许您单独测试管理器及其工作人员类(我将其称为委托,但可能不是)。

编辑——这个答案假设java,因为你提到了servlet。

你有你的经理类,在其中你期望一个接口

class Manager {
    Worker worker;

    Manager(Worker worker) {
        this.worker = workder
    }
}

Worker 是一个接口。它定义了行为,但没有定义实现,

interface Worker {
    public void doesSomething(); //method definition but  no implementation
}

您现在需要创建一个实现。

class WorkerImpl implements Worker {
    // must define a doesSomething() implementation
}

经理只知道它会获得一些工作人员。您可以提供实现该接口的任何类。这是解耦——管理器不绑定到任何特定的实现,它只绑定到工作器的行为。

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

class Manager {
    Worker worker;

    Manager(Worker worker) {
        this.worker = workder
    }
}

Worker is an interface. It defines behavour but not implementation

interface Worker {
    public void doesSomething(); //method definition but  no implementation
}

you now need to create an implementation

class WorkerImpl implements Worker {
    // must define a doesSomething() 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文