Java对象的局部构造

Java对象的局部构造,java,builder,completable-future,abstract-factory,Java,Builder,Completable Future,Abstract Factory,我有一个由几个部分组成的复杂物体。每个部分都需要从不同的服务(REST调用)获取数据 我计划使用AbstractFactorys和Java的CompletableFuture来实现同样的目标 但是,我不确定如何才能做到这一点-有什么建议吗?下面的示例是使用builder和CompletableFuture而不是AbstractFactory,但可能会有所帮助 public class Test { public static void main(String... args) {

我有一个由几个部分组成的复杂物体。每个部分都需要从不同的服务(REST调用)获取数据

我计划使用AbstractFactorys和Java的CompletableFuture来实现同样的目标


但是,我不确定如何才能做到这一点-有什么建议吗?

下面的示例是使用builder和CompletableFuture而不是AbstractFactory,但可能会有所帮助

public class Test {

    public static void main(String... args) {
        CompletableFuture<ComplexObject> completableFuture = CompletableFuture
                                                    .supplyAsync(()-> ComplexObject.builder())
                                                    .thenApply(cob -> cob.withComplexProp1(restService1.getDetails()))
                                                    .thenApply(cob -> cob.withComplexProp2(restService2.getDetails()))
                                                    .thenApply(cob -> cob.withComplexPropN(restServiceN.getDetails()))
                                                    .thenApply(cob -> cob.build());
        try {
            ComplexObject co = completableFuture.get();
        } catch (Exception e) {
            System.err.println("could not build the complex object");
        }
    }


}


class ComplexObject {
    private Object complexProp1;
    private Object complexProp2;
    private Object complexPropN;

    private ComplexObject() {}

    public static ComplexObjectBuilder builder() {
        return new ComplexObjectBuilder();
    }

    public static class ComplexObjectBuilder {

        private Object complexProp1;
        private Object complexProp2;
        private Object complexPropN;

        private ComplexObjectBuilder() {

        }

        public ComplexObjectBuilder withComplexProp1(Object complexProp1) {
            // process the received complexProp1 before setting it into the builder
            this.complexProp1 = complexProp1;
            return this;
        }
        public ComplexObjectBuilder withComplexProp2(Object complexProp1) {
            // process the received complexProp2 before setting it into the builder
            this.complexProp2 = complexProp2;
            return this;
        }
        public ComplexObjectBuilder withComplexPropN(Object complexProp1) {
            // process the received complexPropN before setting it into the builder
            this.complexPropN = complexPropN;
            return this;
        }

        public ComplexObject build() {
            ComplexObject co = new ComplexObject();
            co.complexProp1 = this.complexProp1;
            co.complexProp2 = this.complexProp2;
            co.complexPropN = this.complexPropN;
            return co;
        }
    }
}
公共类测试{
公共静态void main(字符串…参数){
CompletableFuture CompletableFuture=CompletableFuture
.SupplySync(()->ComplexObject.builder())
.thenApply(cob->cob.withComplexProp1(restService1.getDetails()))
.thenApply(cob->cob.withComplexProp2(restService2.getDetails()))
.thenApply(cob->cob.withComplexPropN(restServiceN.getDetails()))
.然后应用(cob->cob.build());
试一试{
ComplexObject co=completableFuture.get();
}捕获(例外e){
System.err.println(“无法构建复杂对象”);
}
}
}
类ComplexObject{
私有对象complexProp1;
私有对象complexProp2;
私有对象complexPropN;
私有ComplexObject(){}
公共静态ComplexObjectBuilder(){
返回新的ComplexObjectBuilder();
}
公共静态类ComplexObjectBuilder{
私有对象complexProp1;
私有对象complexProp2;
私有对象complexPropN;
私有ComplexObjectBuilder(){
}
带有complexProp1的公共ComplexObjectBuilder(对象complexProp1){
//在将接收到的complexProp1设置到生成器中之前,请先对其进行处理
this.complexProp1=complexProp1;
归还这个;
}
带有CompleXProp2的公共ComplexObjectBuilder(对象complexProp1){
//在将接收到的complexProp2设置到生成器中之前,请先对其进行处理
this.complexProp2=complexProp2;
归还这个;
}
带有CompleXPropn的公共ComplexObjectBuilder(对象complexProp1){
//在将接收到的complexPropN设置到生成器中之前,请先对其进行处理
this.complexPropN=complexPropN;
归还这个;
}
公共ComplexObject生成(){
ComplexObject co=新的ComplexObject();
co.complexProp1=this.complexProp1;
co.complexProp2=this.complexProp2;
co.complexPropN=this.complexPropN;
返回公司;
}
}
}
public class Test {

    public static void main(String... args) {
        CompletableFuture<ComplexObject> completableFuture = CompletableFuture
                                                    .supplyAsync(()-> ComplexObject.builder())
                                                    .thenApply(cob -> cob.withComplexProp1(restService1.getDetails()))
                                                    .thenApply(cob -> cob.withComplexProp2(restService2.getDetails()))
                                                    .thenApply(cob -> cob.withComplexPropN(restServiceN.getDetails()))
                                                    .thenApply(cob -> cob.build());
        try {
            ComplexObject co = completableFuture.get();
        } catch (Exception e) {
            System.err.println("could not build the complex object");
        }
    }


}


class ComplexObject {
    private Object complexProp1;
    private Object complexProp2;
    private Object complexPropN;

    private ComplexObject() {}

    public static ComplexObjectBuilder builder() {
        return new ComplexObjectBuilder();
    }

    public static class ComplexObjectBuilder {

        private Object complexProp1;
        private Object complexProp2;
        private Object complexPropN;

        private ComplexObjectBuilder() {

        }

        public ComplexObjectBuilder withComplexProp1(Object complexProp1) {
            // process the received complexProp1 before setting it into the builder
            this.complexProp1 = complexProp1;
            return this;
        }
        public ComplexObjectBuilder withComplexProp2(Object complexProp1) {
            // process the received complexProp2 before setting it into the builder
            this.complexProp2 = complexProp2;
            return this;
        }
        public ComplexObjectBuilder withComplexPropN(Object complexProp1) {
            // process the received complexPropN before setting it into the builder
            this.complexPropN = complexPropN;
            return this;
        }

        public ComplexObject build() {
            ComplexObject co = new ComplexObject();
            co.complexProp1 = this.complexProp1;
            co.complexProp2 = this.complexProp2;
            co.complexPropN = this.complexPropN;
            return co;
        }
    }
}