Firstly, I find your model a bit strange for ATMCard and Account :
- not a
PIN associated with an ATMCard , not an Account ? - is not
custName associated with Account ?
Then a ratio of 1-1 means that you will have one of them:
- The class
Account has an ATMCard member ATMCard - class
ATMCard has a member of type Account - both of the above.
- None of the above, but recipients who will receive the associated object based on the identifier. For example, you can have
ATMCard$getAccount() , which will retrieve the associated Account based on accountNo .
It really depends on the required model logic.
As @NickHolt points out, I would go for a one-way relationship that you can initialize through a factory, e.g.
public static ATMCard createCard(String name, int accNo, int pin, int initBal) { Account acc = new Account(name, accNo, initBal); ATMCard card = new ATMCard(pin); card.setAccount(acc); return card; }
You can have secure ATMCard and Account constructors to enable the use of the public factory method.
Note. You can use a framework like Spring or Guice to provide this kind of factory and injection service.
source share