如何改善建筑模式? [英] How to improve the builder pattern?

查看:145
本文介绍了如何改善建筑模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我搜索了一种初始化复杂对象的方式,而不会将很多参数传递给构造函数。我尝试使用构建器模式,但我不喜欢这个事实,如果我真的设置了所有需要的值,我无法在编译时检查。

Recently I searched for a way to initialize a complex object without passing a lot of parameter to the constructor. I tried it with the builder pattern, but I don't like the fact, that I'm not able to check at compile time if I really set all needed values.

当我使用构建器模式创建我的复杂对象时,创建更 ,因为更容易看到使用什么参数:

When I use the builder pattern to create my Complex object, the creation is more "typesafe", because it's easier to see what an argument is used for:

new ComplexBuilder()
        .setFirst( "first" )
        .setSecond( "second" )
        .setThird( "third" )
        ...
        .build();

但是现在我有这个问题,我很容易错过一个重要的参数。我可以在 build()方法中检查它,但这只是在运行时。在编译时没有什么可以警告我,如果我错过了某些东西。

But now I have the problem, that I can easily miss an important parameter. I can check for it inside the build() method, but that is only at runtime. At compile time there is nothing that warns me, if I missed something.

现在我的想法是创建一个建筑师,如果我错过了一个需要的参数,提醒我。我的第一个尝试看起来像这样:

Now my idea was to create a builder, that "reminds" me if I missed a needed parameter. My first try looks like this:


public class Complex {
    private String m_first;
    private String m_second;
    private String m_third;

    private Complex() {}

    public static class ComplexBuilder {
        private Complex m_complex;

        public ComplexBuilder() {
            m_complex = new Complex();
        }

        public Builder2 setFirst( String first ) {
            m_complex.m_first = first;
            return new Builder2();
        }

        public class Builder2 {
            private Builder2() {}
            Builder3 setSecond( String second ) {
                m_complex.m_second = second;
                return new Builder3();
            }
        }

        public class Builder3 {
            private Builder3() {}
            Builder4 setThird( String third ) {
                m_complex.m_third = third;
                return new Builder4();
            }
        }

        public class Builder4 {
            private Builder4() {}
            Complex build() {
                return m_complex;
            }
        }
    }
}

正如你所看到的,构建器类返回不同的内部构建器类。每个内部构造器类只提供一个setter方法,最后一个只提供一个build()方法。

As you can see, each setter of the builder class returns a different internal builder class. Each internal builder class provides exactly one setter method and the last one provides only a build() method.

现在再次构造一个对象,如下所示:


Now the construction of an object again looks like this:

new ComplexBuilder()
    .setFirst( "first" )
    .setSecond( "second" )
    .setThird( "third" )
    .build();

...但没有办法忘记所需的参数。编译器不会接受它。

...but there is no way to forget a needed parameter. The compiler wouldn't accept it.

如果我有可选参数,我会使用最后一个内部构建器类 Builder4 将它们设置为传统构建器,返回自己。

If I had optional parameters, I would use the last internal builder class Builder4 to set them like a "traditional" builder does, returning itself.


  • 这是一个众所周知的模式吗?是否有特殊名称?

  • 您是否看到任何陷阱?

  • 您是否有任何改进实施的想法 - 在更少的意义上代码行

推荐答案

不,这不是新的。您实际在做的是通过扩展标准构建器来创建一种 DSL 支持分支的模式,这是确保构建器不会对实际对象产生一组冲突设置的好方法。

No, it's not new. What you're actually doing there is creating a sort of a DSL by extending the standard builder pattern to support branches which is among other things an excellent way to make sure the builder doesn't produce a set of conflicting settings to the actual object.

个人我认为这是对构建器模式的极大推广,您可以使用它来进行各种有趣的事情,例如在工作中,我们为我们的数据完整性测试提供了DSL构建器,这些测试允许我们执行诸如 assertMachine() 。.usesElectricity()和()makesGrindingNoises()whenTurnedOn(); 。好的,也许不是最好的例子,但是我想你得到了解。

Personally I think this is a great extension to builder pattern and you can do all sorts of interesting things with it, for example at work we have DSL builders for some of our data integrity tests which allow us to do things like assertMachine().usesElectricity().and().makesGrindingNoises().whenTurnedOn();. OK, maybe not the best possible example but I think you get the point.

这篇关于如何改善建筑模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆