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
Stock
have a reference to it'sStockDetails
andStockDetails
have a reference to it'sStock
? - If the
StockDetails
doesn't have a reference to it'sStock
, that quite makes no sense, because theStockDetails
object is meaningless without the correspondingStock
. However, if it does have, it's pointless and weird to obtain theStock
from it's own details, ie.stockDetails.getStock()
. - Should the
StockDetails
be an inner class of theStock
? This again is not ideal, because I would like to access theStockDetails
class outside of theStock
. - It's even pointless to be able to instantiate a
StockDetails
object, without previously having aStock
object. The two should somehow be instantiated together, as they co-exist.
What would be the most sensible way to achieve that kind of relationship?
Aucun commentaire:
Enregistrer un commentaire