Reputation: 2735
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:
Stock
have a reference to it's StockDetails
and StockDetails
have a reference to it's Stock
?StockDetails
doesn't have a reference to it's Stock
, that quite makes no sense, because the StockDetails
object is meaningless without the corresponding Stock
. However, if it does have, it's pointless and weird to obtain the Stock
from it's own details, ie. stockDetails.getStock()
.StockDetails
be an inner class of the Stock
? This again is not ideal, because I would like to access the StockDetails
class outside of the Stock
.StockDetails
object, without previously having a Stock
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?
Upvotes: 2
Views: 49
Reputation: 73366
StockDetail
a Stock
?If Stock
is a general concept that can exists on its own, but you need to specialize it for your needs with additional informations and functionality, then you could consider inheritance. The same object will then act for Stock
and for StockDetail
:
public class StockDetail extends Stock { ... }
Consequence:
Stock
is independent of class StockDetail
StockDetail
depends on Stock
that it specializesStock
or a StockDetail
, and that this will not evolve over time.SockDetail
a decorator of Stock
?If a StockDetail
should always behave as a Stock
, but creation you can't tell which one to create, then you can think of using the decorator pattern.
Initially you'd create a Stock
object. StockDetail
would inherit from Stock
, and hold a reference to the corresponding Stock
. Wherever you need the details, you'd use the decorator object.
Consequence:
Stock
is independent of class StockDetail
StockDetail
depends on Stock
Stock
object.StockDetail
object that refers to the Stock
object, and use the StockDetail
instead of the Stock
.StockDetail
a component of Stock
?This would be the candidate by default according to the principle of composition over inheritance. You just add a StockDetail
member to the Stock
.
If a StockDetail
object is dependent of a Stock
object, i.e. if it can't exist without the corresponding Stock
, then the easiest approach would be to always access StockDetail
via its owner, the Stock
object. In this case a reverse link would not be needed. In the DDD terminology, you'd say that both objects belong to the same aggregate and Stock
is the aggregate root.
Consequence:
StockDetail
is independent of class Stock
Stock
depends on StockDetail
Stock
object.StockDetail
object for a Stock
object.Stock
and StockDetails
independent but related objects ?If a Stock
object has a StockDetail
object, but if at the same time that StockDetail
object could be used independently of the Stock
object, you would need a bidirectional relation.
In this case you would need a reference in Stock
to StockDetail
, and a reference in StockDetail
to Stock
. And you would need to ensure consistency of both links.
Consequence:
Stock
and StockDetail
are strongly coupled and interdependentUpvotes: 2