Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Java构建fluent API为数据库构建testdata_Java_Dsl_Builder_Fluent - Fatal编程技术网

用Java构建fluent API为数据库构建testdata

用Java构建fluent API为数据库构建testdata,java,dsl,builder,fluent,Java,Dsl,Builder,Fluent,我正在尝试用Java制作一个小型DSL,我可以用它在数据库中填充testdata。我想使用的语言如下 createRowInTableA(). createRowInTableB(). createRowInTableA(). createRowInTableB(). createRowInTableC(). end(); 表的创建顺序很重要,例如tableB依赖于tableA,tableC依赖于tableA和tableB。因此,我想使创建tableB的选项仅在创建ta

我正在尝试用Java制作一个小型DSL,我可以用它在数据库中填充testdata。我想使用的语言如下

createRowInTableA().
   createRowInTableB().
createRowInTableA().
   createRowInTableB().
      createRowInTableC().
end();
表的创建顺序很重要,例如tableB依赖于tableA,tableC依赖于tableA和tableB。因此,我想使创建tableB的选项仅在创建tableA后直接可用,等等。我已经开始创建描述DSL的接口,但我不知道如何实际实现接口,以实现我正在寻找的嵌套行为类型。这就是接口的外观

public interface End {
    public void sendTestData(); 
}

public interface TableA extends End {
   public Builder createRowInTableA();
}

public interface TableB extends TableA {
   public Builder createRowInTableB();
}

public interface TableC extends TableB {
   public Builder createRowInTableC();
}
然而,当我开始使用builder模式实现这种语言来创建一个流畅的API时,我想要的层次结构就消失了

public class DBBuilder implements TableC {
   static class Builder {
      public Builder createRowInTableA(){...}
      public Builder createRowInTableB(){...}
      public Builder createRowInTableC(){...}
   }
}

您可以使用一组接口和类适配器:

public interface canCreateTableAIf{
   public DBBuilderB createRowInTableA()
}  

public interface canCreateTableBIf{
   public DBBuilderC createRowInTableB()
}  

public interface canCreateTableCIf{
   public DBBuilderD createRowInTableC()
}  


public class canCreateTableA implements canCreateTableAIf (){
  public DBBuilderB createRowInTableA(){
    ...
  }
}

public class canCreateTableB implements canCreateTableBIf (){
  public DBBuilderC createRowInTableB(){
    ...
  }
}

public class DBBuilderRoot extends canCreateTableA {
}


public class DBBuilderB  extends canCreateTableB {
}

public class DBBuilderBCD  extends canCreateTableB,canCreateTablec,canCreateTableD {
}

这并不复杂。但我会检查是否有比使用fluent构建器更好的方法。例如,Java8提供了闭包。这是我的建议。我还没有编译和测试它。这个想法应该行得通,但可能有语法错误

public class ABuilder 
{
    private BBuilder subBuilder;

    public ABuilder()
    {
      subBuilder = new BBuilder(this);
    }

    public BBuilder createRowForA()
    {
       // your code
       return this.subBuilder;
    }

    public void end()
    {
      // send test data
    }
}
x

x

然后你可以做:

(new ABuilder())
    .createRowForA()
      .createRowForB()
        .createRowForC()
        .end()
      .end()
     .end();

(new ABuilder())
    .createRowForA()
      .createRowForB()
        .end()
      .createRowForB()
        .createRowForC()
        .end()
       .end()
     .end();

我相信你会看到更多的例子

您可以有很多构建器类,每个类都有一组有限的方法;方法可能会返回另一个生成器类型。这可能会变得太复杂,而且可能不值得。
public class CBuilder 
{
    private BBuilder parentBuilder;

    public CBuilder( BBuilder parentBuilder )
    {
      this.parentBuilder = parentBuilder;
    }

    public CBuilder createRowForC()
    {
       // your code

       // I Assume you want to be able to write more than 1 C-row
       return this;
    }

    public BBuilder end()
    {
      return this.parentBuilder;
    }
}
(new ABuilder())
    .createRowForA()
      .createRowForB()
        .createRowForC()
        .end()
      .end()
     .end();

(new ABuilder())
    .createRowForA()
      .createRowForB()
        .end()
      .createRowForB()
        .createRowForC()
        .end()
       .end()
     .end();