wesleyy
wesleyy

Reputation: 2735

Sensible one-to-one class relationship

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:

  1. Should the Stock have a reference to it's StockDetails and StockDetails have a reference to it's Stock?
  2. If the 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().
  3. Should the 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.
  4. It's even pointless to be able to instantiate a 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

Answers (1)

Christophe
Christophe

Reputation: 73366

Is a 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:

  • the class Stock is independent of class StockDetail
  • class StockDetail depends on Stock that it specializes
  • whenever you create an object, you need to know for sure if it's a Stock or a StockDetail, and that this will not evolve over time.

Is 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:

  • the class Stock is independent of class StockDetail
  • class StockDetail depends on Stock
  • you always first create a Stock object.
  • you can create a StockDetail object that refers to the Stock object, and use the StockDetail instead of the Stock.

Is 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:

  • the class StockDetail is independent of class Stock
  • class Stock depends on StockDetail
  • you always first create a Stock object.
  • you can create a StockDetail object for a Stock object.

Are 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:

  • Classes Stock and StockDetail are strongly coupled and interdependent
  • When you access the object of one class, you can access to the corresponding object of the other class

Upvotes: 2

Related Questions