What is the correct model for a retail store? e.g. Company sells Products from Stores.
A company can have many stores and sell many products. The products aren’t necessarily tied to each store, or so I would’ve thought.
I have a basic assignment, which asks that I design a system that calculates the economic order quantity of a product. We are supposed to implement a structure that will be suitable for a later assignment (for which we have no details..), and the advised structure is as follows:
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;
}
}
This seems to violate all of the little I know about OOP. Methods that pass data down through the hierarchy – duplicating parameters between objects? What am I missing?
You’re correct to be concerned in that it would be unusual to initialise a hierarchy of objects by passing in all the parameters from the top down.
Your lecturer may be hinting that you implement something along the lines of the Composite pattern, whereby each class in the hierarchy shares a common method (e.g.
getValue()). In the case of aProduct(i.e. leaf node) this would simply return the product’s value, whereas in the case of aStoreorCompanyit would iterate over the constituentProducts (orStores), callinggetValue()and summing the result.The key difference between this and what you’ve written above is that you would typically initialise each
Productindividually via its constructor, rather than by passing in the data from another object. If the product is immutable you might choose to mark its fields asfinal. You might then choose to add utility methods to other classes in the hierarchy (e.g.moveProduct(Store store1, Store store1)); In other words the other classes would then exhibit behaviour rather than just being “data containers”.Example