【问题标题】:Object Hierarchy对象层次结构
【发布时间】:2012-04-04 07:34:40
【问题描述】:

零售店的正确模式是什么?例如公司从商店销售产品。

一家公司可以拥有许多商店并销售许多产品。产品不一定与每家商店相关联,或者我会这么认为。

我有一个基本任务,要求我设计一个系统来计算产品的经济订货量。我们应该实现一个适合以后分配的结构(我们没有详细信息..),建议的结构如下:

public class Company {
    private Store store;

    public static void main(String[] args)
    {
        Store storeOne = new Store();

        storeOne.setEOQRelevantValues(1, etc..);
    }
}

public class Store {
    private Product product;

    //..

    public setEOQRelevantValues(int eoqRelevantValues)
    {
        Product.setEOQRelevantValues(eoqRelevantValues);
    }
}

public class Product{
    private int eoqRelevantValues;

    //..

    public setEOQRelevantValues(int eoqRelevantValues)
    {
        this.eoqRelevantValues = eoqRelevantValues;
    }

    public int calculateEOQ()
    {
        //do stuff with this.eoqRelevantValues..
        return EOQ;
    }
}

这似乎违反了我对 OOP 的所有了解。通过层次结构向下传递数据的方法 - 在对象之间复制参数?我错过了什么?

【问题讨论】:

  • 这看起来是一个拙劣的组合设置,绝对不是一个正确有用的对象图设置。例如,我不明白为什么“产品是 [应该是] 商店”或“商店是 [应该是] 产品”。那是真的显示/建议的吗?
  • @pst - 是的,下一个任务将需要多个商店和这些商店下的产品。

标签: java object hierarchy


【解决方案1】:

您的担心是正确的,因为通过自上而下传递所有参数来初始化对象层次结构是不寻常的。

您的讲师可能会暗示您按照复合模式实现某些东西,即层次结构中的每个类共享一个公共方法(例如getValue())。在Product(即叶节点)的情况下,这将简单地返回产品的值,而在StoreCompany 的情况下,它将遍历组成Products(或Stores ),调用getValue() 并对结果求和。

这与您在上面编写的内容之间的主要区别在于,您通常会通过其构造函数单独初始化每个Product,而不是通过从另一个对象传入数据。如果产品是不可变的,您可以选择将其字段标记为final。然后,您可以选择将实用程序方法添加到层次结构中的其他类(例如moveProduct(Store store1, Store store1));换句话说,其他类将表现出行为,而不仅仅是“数据容器”。

示例

/**
 * Optional interface for uniting anything that can be valued.
 */
public interface Valuable {
  double getValue();
}

/**
 * Company definition.  A company's value is assessed by summing
 * the value of each of its constituent Stores.
 */
public class Company implements Valuable {
  private final Set<Store> stores = new HashSet<Store>();

  public void addStore(Store store) {
    this.stores.add(store);
  }

  public void removeStore(Store store) {
    this.stores.remove(store);
  }

  public double getValue() {
    double ret = 0.0;

    for (Store store : stores) {
      ret += store.getValue();
    }

    return ret;
  }
}

/**
 * Store definition.  A store's value is the sum of the Products contained within it.
 */
public class Store implements Valuable {
  private final List<Product> products = new LinkedList<Product>();

  public void addProduct(Product product) {
    this.products.add(product);
  }

  public void removeProduct(Product product) {
    this.products.remove(product);
  }

  public double getValue() {
    double ret = 0.0;

    for (Product product : products) {
      ret += product.getValue();
    }

    return ret;
  }
}

/**
 * Product definition.  A product has a fixed inherent value.  However, you could
 * always model a product to depreciate in value over time based on a shelf-life, etc.
 * In this case you might wish to change the Valuable interface to accept a parameter;
 * e.g. depreciationStrategy.
 */
public class Product implements Valuable {
  private final double value;

  public Product(double value) {
    this.value = value;
  }

  public double getValue() {
    return value;
  }
}

【讨论】:

  • 感谢您的回复。我阅读了复合模式,但在这个阶段它似乎有点超出我的水平,我正在努力理解你的意思。您是否有机会提供一个简短的代码示例? :) 谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
相关资源
最近更新 更多