简体   繁体   English

Java类之间的一对一关系

[英]one to one relationship between classes in java

I have to implement following classes in Java SE but I cannot figure out that how can I achieve the one-to-one relationship between ATMCard and Account. 我必须在Java SE中实现以下类,但无法弄清楚如何实现ATMCard和Account之间的一对一关系。 I have researched (may be with wrong keywords) but could not find anything. 我已经研究过(可能带有错误的关键字),但找不到任何东西。 Thank you in advance.. 先感谢您..

问题中提到的类图

First, I find your model a bit strange for ATMCard and Account : 首先,我发现您的模型对于ATMCardAccount有点奇怪:

  • isn't the PIN related to the ATMCard rather than to the Account ? PINATMCard而非Account吗?
  • isn't the custName related the Account ? custNameAccount是否相关?

Then, the 1-1 relationship means you will have one of these: 然后,1-1关系意味着您将具有以下之一:

  • the Account class has a member of type ATMCard Account类具有ATMCard类型的成员
  • the ATMCard class has a member of type Account ATMCard类的成员类型为Account
  • both above relations. 以上两种关系。
  • none of the above, but getters that will fetch the related entity based on an ID. 以上都不是,但是将根据ID获取相关实体的吸气剂。 For instance, you can have a ATMCard$getAccount() that will retrieve the related Account based on the accountNo . 例如,您可以拥有一个ATMCard$getAccount() ,该ATMCard$getAccount()将根据accountNo检索相关的Account

It really depends on the model logic you need. 这实际上取决于您需要的模型逻辑。

As @NickHolt suggests, I would go for a one-way relation ship you can initialise through a factory, eg 正如@NickHolt所建议的那样,我将选择一种可以通过工厂初始化的单向关系船,例如

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 ATMCard and Account constructors protected to enforce the use of the public factory method. 您可以保护ATMCardAccount构造函数以强制使用public factory方法。

Note: you can use a framework like Spring or Guice to provide this kind of factory and injection service. 注意:可以使用SpringGuice类的框架来提供这种工厂注入服务。

要在ATMCardAccount之间创建一对一关系,您必须在ATMCard类中创建Account实例。

The way I see this is ATM doesn't need to be related to ATMCard or the Account . 我认为这是ATM的方式不需要与ATMCardAccount Think about what happens you bank ATMCard is used in ATM of other bank? 想一想您的银行ATMCard在其他银行的ATM中使用会怎样? Or you bank doesn't allow the ATMCard be used in other bank ATM 否则您的银行不允许将ATMCard用于其他银行的ATM

These should part of withdrawal operation of the ATM. 这些应该是自动柜员机提款操作的一部分。

You could have a bidirectional relationship from Account to ATMCard 您可能有一个从AccountATMCard的双向关系

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 两个类之间无法建立“一对一”关系 - Unable to make 'one-to-one' relationship between the two classes 具有一对多关系的Java类中的循环依赖关系 - Circular dependencies in a Java classes with a one-to-many relationship 查找 java 类之间的关系 - Finding relationship between java classes Java:两个相似的类,一个不可变-在它们之间共享代码 - Java : Two similar classes, one immutable - sharing code between them 接口/抽象类之间的Java关系 - Java Relationship between interfaces/abstract classes 如何在Java中定义类之间的关系? - How to defind the relationship between classes in java? 我如何将这两个类链接在一起以显示 Person 和 Address 之间的一对多关系 - How would I link the two classes together to show the one to many relationship between Person and Address 如何在Java中存储一对一关系 - How to Store a One to One Relationship in Java 在Java中实现一对一关系 - Implement one-to-one relationship in java 如何使用Spring Boot JPA在这两个类之间建立一对一的关系?另外,如何保存实体? - How can i establish a one to one relationship between this two classes using spring boot jpa?, also, how do i save the entities?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM