【问题标题】:Java Object construction in partsJava 对象构造部分
【发布时间】:2018-10-16 10:41:22
【问题描述】:

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

public class Resource {
     int quantity;            // Service 1
     String name;             // Service 2
     Price price;             // Service 2, 3
     ...
}

public class Price {
     double value;              // Service 2
     String currency;           // Service 2
     int discount_percentage;   // Service 3
}

我打算使用 AbstractFactories 和 Java 的 CompletableFuture 来完成同样的任务。

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

【问题讨论】:

    标签: java builder completable-future abstract-factory


    【解决方案1】:

    下面的示例是使用 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;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多