gpt4 book ai didi

unit-testing - 摆脱子组件对象的 'new' 运算符

转载 作者:行者123 更新时间:2023-12-04 11:43:19 25 4
gpt4 key购买 nike

我一直在阅读 Misko Hevery 的 classic articles关于依赖注入(inject),基本上是“将对象图创建代码与代码逻辑分离”。

主要思想似乎是“摆脱‘新’操作符”,将它们放在专用对象(‘工厂’)中,并注入(inject)您所依赖的一切。”

现在,我似乎无法理解如何使这与由几个其他组件组成的对象一起工作,并且其工作是将这些组件与外部世界隔离。

跛脚的例子

一个 View 类来表示几个字段和一个按钮的组合。所有组件都依赖于图形 ui 上下文,但您希望将其隐藏在每个子组件的界面后面。

所以像(在伪代码中,语言并不重要,我猜):


class CustomView() {

public CustomView(UIContext ui) {
this.ui = ui
}

public void start() {

this.field = new Field(this.ui);
this.button = new Button(this.ui, "ClickMe");

this.button.addEventListener(function () {
if (field.getText().isEmtpy()) {
alert("Field should not be empty");
} else {
this.fireValueEntered(this.field.getText());
}
});
}

// The interface of this component is that callers
// subscribe to "addValueEnteredListener"..)
public void addValueEnteredListener(Callback ...) {
}

public void fireValueEnteredListener(text) {
// Would call each listeners in turn
}
}

调用者会做类似的事情:


// Assuming a UIContext comes from somewhere...
ui = // Wherever you get UI Context from ?
v = new CustomView(ui);
v.addValueEnteredListener(function (text) {
// whatever...
});


现在,这段代码有三个"new"操作符,我不确定 Misko(和其他 DI 支持者)主张删除哪一个,或者如何删除。

摆脱 new Field() 和 new Button()

只需注入(inject)它

我不认为这里的想法是实际注入(inject) Field 和 Button 的实例,这可以通过以下方式完成:

class CustomView() {

public CustomView(Field field, Button button) {
this.field = field;
this.button = button;
}

public void start() {

this.button.addEventListener(function () {
if (field.getText().isEmtpy()) {
alert("Field should not be empty");
} else {
this.fireValueEntered(this.field.getText());
}
});
}

// ... etc ...

这当然让组件的代码更轻量了,而且它实际上隐藏了 UI 的概念,因此 MetaForm 组件在可读性和可测试性方面得到了明显的提升。

但是,现在创建这些东西的责任在于客户端:


// Assuming a UIContext comes from somewhere...

ui = // wherever ui gets injected from

form = new Form(ui);
button = new Button(ui);

v = new CustomView(form, button);
v.addValueEnteredListener(function (text) {
// whatever...
});


这对我来说听起来真的很麻烦,尤其是因为客户知道必须了解类(class)的所有内部人员,这听起来很愚蠢。

妈妈知道,给她注入(inject)

文章似乎提倡的是注入(inject)一个工厂来创建组件元素。

class CustomView() {

public CustomView(Factory factory) {
this.factory = factory;
}

public void start() {

this.field = factory.createField();
this.button = factory.createButton();

this.button.addEventListener(function () {
if (field.getText().isEmtpy()) {
alert("Field should not be empty");
} else {
this.fireValueEntered(this.field.getText());
}
});
}

// ... etc ...

然后对于调用者来说一切都会变得很好,因为它只需要从某个地方获取工厂(并且这个工厂将是唯一知道 UI 上下文的人,所以请为解耦欢呼吧。)


// Assuming a UIContext comes from somewhere...

factory = // wherever factory gets injected from

v = new CustomView(factory);
v.addValueEnteredListener(function (text) {
// whatever...
});


一个可能的缺点是,在测试 MetaForm 时,您通常必须使用“模拟”工厂……创建字段和按钮类的模拟版本。但显然还有另一个缺点......

哟'工厂太胖了!!

工厂会有多大?如果您严格遵循该模式,那么您想在运行时在应用程序中创建的每个单独的组件(UI 通常就是这种情况,对)都必须在至少一个工厂中获得自己的 createXXXXX 方法。

因为现在你需要:
  • Factory.createField 创建字段
  • Factory.createButton 创建按钮
  • Factory.createMetaForm 在客户端(比如 MetaEditPage 想要使用一个)时创建字段、按钮和 MetaForm
  • 显然是客户端的 Factory.createMetaEditPage ..
  • ......还有它的海龟。

  • 我可以看到一些简化此操作的策略:
  • 尽可能将在“启动”时创建的图部分与运行时创建的部分分开(前者使用像 Spring 这样的 DI 框架,后者使用工厂)
  • 将工厂分层,或在同一工厂中搭配相关对象(UIWidgetFactory 对 Field 和 Button 有意义,但您会将其他工厂放在哪里?在链接到应用程序的工厂中?到其他逻辑级别?)

  • 我几乎可以听到 C 人员的所有笑话,如果不调用 ApplicationProcessFactoryCreator.createFactory().createProcess().startApplication() 废话链,任何 Java 应用程序都无法做任何事情......

    所以我的问题是:
  • 我在这里完全遗漏了一点?
  • 如果不是,您会建议采用哪种策略使事情变得可以忍受?

  • 附录:为什么我不确定依赖注入(inject)会有所帮助

    假设我决定使用依赖注入(inject),以及类似 guice 的框架。我最终会编写这样的代码:

    class CustomView

    @Inject
    private Field fiedl;

    @Inject
    private Button button;

    public void start() {
    this.button.addEventListener(....

    // etc...

    然后什么 ?

    我的“组合根”将如何利用它?我当然不能为 Field 和 Button 配置“单例”(带有小写的 's',就像在“类的单个实例”中一样)(因为我想创建与 MetaForm 实例一样多的它们的实例?

    使用 Provider 是没有意义的,因为我的问题不是我想创建哪个按钮实例,而是我最近想创建它,一些配置(例如它的文本)只对这个表单有意义.

    对我来说 DI 不会有帮助,因为我是组件的新部分而不是依赖项。我想我可以将任何子组件转换为依赖项,然后让框架注入(inject)它们。只是注入(inject)子组件对我来说看起来非常人为和直观,在这种情况下......所以再次,我必须错过一些东西;)

    编辑

    特别是,我的问题是我似乎无法理解您将如何测试以下场景:

    “当我单击按钮时,如果字段为空,则应该有错误”。

    如果我注入(inject)按钮,这是可行的,这样我就可以手动调用它的“fireClicked”事件 - 但感觉有点傻。

    另一种方法是执行 view.getButton().fireClicked() ,但这看起来有点难看......

    最佳答案

    好吧,您可以使用一些 DI 框架(Spring 或 Guice)并完全摆脱工厂方法。只需在 field/constructor/set 方法上添加一些注释,DI Framework 就会完成工作。在单元测试中使用模拟框架。

    关于unit-testing - 摆脱子组件对象的 'new' 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12512985/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com