Let's say I have a class Stock representing some basic stock data, and then the additional class StockDetails providing some additional details of a specific stock.
Stock.java
public class Stock {
private int stockId;
private String stockCode;
private String stockName;
private StockDetail stockDetail;
public Stock() {
}
public Stock(String stockCode, String stockName) {
this.stockCode = stockCode;
this.stockName = stockName;
}
// ...
}
StockDetails.java
public class StockDetail {
private Stock stock; // yes or no?
private String compName;
private String compDesc;
private String remark;
private Date listedDate;
public StockDetail() {
}
public StockDetail(String compName, String compDesc,
String remark, Date listedDate) {
this.compName = compName;
this.compDesc = compDesc;
this.remark = remark;
this.listedDate = listedDate;
}
// ...
}
Now I am not sure what would be the best way to design this kind of relationship. My concerns are:
- Should the
Stockhave a reference to it'sStockDetailsandStockDetailshave a reference to it'sStock? - If the
StockDetailsdoesn't have a reference to it'sStock, that quite makes no sense, because theStockDetailsobject is meaningless without the correspondingStock. However, if it does have, it's pointless and weird to obtain theStockfrom it's own details, ie.stockDetails.getStock(). - Should the
StockDetailsbe an inner class of theStock? This again is not ideal, because I would like to access theStockDetailsclass outside of theStock. - It's even pointless to be able to instantiate a
StockDetailsobject, without previously having aStockobject. The two should somehow be instantiated together, as they co-exist.
What would be the most sensible way to achieve that kind of relationship?