有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当新对象是原始对象的一部分时,java会将对实例的引用传递给另一个对象吗?

当新实例将成为原始实例的一部分时,将父类的this实例传递到另一个对象的构造函数时(从设计角度来看),是否会有任何缺陷

public class Order extends AbstractOrder {

  private final OrderExecutionStrategy executionStrategy;

  private Order() {
    this.executionStrategy = new OrderExecutionStrategy(this);
  }

  // the implementation omitted for brevity...

}

我需要从OrderExecutionStrategy类中的父实例访问数据

public class OrderExecutionStrategy extends AbstractOrderExecutionStrategy  {

  public OrderExecutionStrategy(final Order order) {
    super(order);
  }

  @Override
  public Optional<OrderPortion> executePortion(final BigDecimal newPrice, final TradeOrders orders) {

    AssertUtils.notNull(orders, "orders");
    AssertUtils.isGtZero(newPrice, "newPrice");

    if (ComparisonUtils.equals(getOrder().getPrice(), newPrice)) {

      final BigDecimal reaminingAmount = this.getOrder().summary().getRemainToFill();

      if (ValidationUtils.isGtZero(reaminingAmount)) {
        return Optional.of(new OrderPortion(this.getOrder().getId(), reaminingAmount));
      }

    }

    return Optional.empty();

  }

}

共 (1) 个答案

  1. # 1 楼答案

    我看不出这里面有任何设计缺陷

    然而,有几个警告:

    1. 我说的是设计缺陷,而不是实现缺陷

      "I am thinking that these two instances could negatively affect each other, an endless loop or something in that sense."

      这些将是实现缺陷(又名bug),而不是设计缺陷。需要更多的上下文来检查这种情况

    2. 你只向我们展示了设计的一小部分,几乎没有线索表明这与“大局”是如何契合的