2012-04-04 92 views
1

什么是零售店的正确模式?例如公司从商店出售产品。对象等级

一家公司可以有很多商店和销售许多产品。产品不一定与每家商店绑在一起,或者我可能会这么想。

我有一个基本的任务,它要求我设计一个系统来计算产品的经济订货量。我们应该实施,这将是适用于以后的分配(我们有没有详细说明。)的结构,并建议结构如下:

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。通过层次传递数据的方法 - 在对象之间复制参数?我错过了什么?

+0

这看起来是一个拙劣* *组合物设置,绝对不是一个正确的有用对象的图形化设置。例如,我不明白为什么“产品[应该是]商店”或“商店是[应该是]产品”。那*真的*显示/建议了什么? – 2012-04-04 07:44:59

+0

@pst - 是的,下一项任务将需要这些商店下的多个商店和产品。 – tkf144 2012-04-04 11:08:20

回答

2

你是正确的担心,通过从顶部向下传递所有参数来初始化对象层次结构是不正常的。

您的讲师可能暗示您沿着复合模式的行执行某些操作,因此层次结构中的每个类共享一个常用方法(例如getValue())。在Product(即叶节点)的情况下,这将简单地返回产品的值,而在StoreCompany的情况下,它将遍历组分Product(或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; 
    } 
} 
+0

谢谢你的回应。我已经阅读了复合模式,但在这个阶段它似乎略高于我,我正在努力去理解你的意思。有没有可能提供一个简短的代码示例? :)谢谢 – tkf144 2012-04-04 11:10:31

+0

@Thomas:添加示例。 – Adamski 2012-04-04 11:48:21