[英]ClassDiagramm 0..1 to 0..1 relation missing steps to convert into java code
我想通過uml / class-diagram編寫Java代碼。 但是我不確定它或如何為關系0..1到0..1編寫它
我沒有找到有關此關系的任何信息。
1到0..1是可能的,我知道如何創建它。 這是我的類圖,關系為0..1到0..1:
我為此編寫了代碼。
public class IssueWithBankStuff {
Iban Iban;
Bic bic;
Customer customer;
Date contractIban;
IssueWithOtherStuff other;
public IssueWithBankStuff() {
}
public ContractForm getContractForm() {
return other.gethContractForm();
}
public void setContractForm(ContractForm contractForm) {
other.gethContractForm(contractForm);
}
public isHolding() {
return other.isHolding();
}
public void setHolding(Boolean hold) {
other.setHolding(hold);
}
public isGeneralCorperateForm() {
return other.isGeneralCorperateForm();
}
public void setHolding(Boolean generalCorperateForm) {
other.setGeneralCorperateForm(generalCorperateForm);
}
public getStartDate() {
return other.getStartDate();
}
public void setContractForm(Date startDate) {
other.setStartDate(startDate);
}
//class specific getters and setters
}
public IssueWithOtherStuff {
ContractForm contractForm;
Boolean holding;
Boolean generalCorperateForm
Date startDate;
IssueWithBankStuff iban;
public IssueWithOtherStuff () {
}
public void setIban(Iban ib) {
iban.setIban(ib);
}
public Iban getIban () {
return iban.getIban();
}
public void setBic(Bic bic) {
iban.setBic(bic);
}
public Bic getBic () {
return iban.getBic();
}
public void setCustomer(Customer customer) {
iban.setCustomer(customer);
}
public Customer getCustomer () {
return iban.getCustomer();
}
public void setContractIban(Date contractIban) {
iban.setContractIban(contractIban);
}
public Date getContractIban () {
return iban.getContractIban();
}
//getters and setters
}
0..1
表示可選項。 通常,在編寫代碼時,您將使用名為Null,None,Nil,Void,Optional等的特定於語言的構造。不確定Java,但我記得有一些類似的東西。
Java中用於管理A -0..1----0..1- B
的示例,當在兩側的任一側完成設置時,在另一側也進行了設置:
A.java
public class A {
private B b;
public void setB(B v) {
if (b != v) {
B prev = b;
b = v; // set relation here
if (prev != null) {
// remove relation on other side
prev.setA(null);
// if prev->a was this now b is null, set it again
b = v;
}
if (b != null)
// add relation on other side
b.setA(this);
}
}
public B getB() { return b; }
}
B.java
public class B {
private A a;
public void setA(A v) {
if (a != v) {
A prev = a;
a = v; // set relation here
if (prev != null) {
// remove relation on other side
prev.setB(null);
// if prev->b was this now a is null, set it again
a = v;
}
if (a != null)
// add relation on other side
a.setB(this);
}
}
public A getA() { return a; }
}
並檢查Main.java:
class Main {
public static void main(String[] args) {
A a = new A();
B b = new B();
a.setB(b);
System.out.println((a.getB() == b) && (b.getA() == a));
a.setB(null);
System.out.println((a.getB() == null) && (b.getA() == null));
b.setA(a);
System.out.println((a.getB() == b) && (b.getA() == a));
b.setA(null);
System.out.println((a.getB() == null) && (b.getA() == null));
B bb = new B();
a.setB(b);
a.setB(bb);
System.out.println(b.getA() == null);
System.out.println((a.getB() == bb) && (bb.getA() == a));
}
}
執行一直寫為真
請因為我不是Java程序員,如果有些愚蠢的話不要對我太粗魯^^
無論您的樣品(IssueWithBankStuff或IssueWithOtherStuff)在兩班曾經有它們的引用彼此集 (例如有在任一類攜帶到其他的引用沒有構造函數的參數,也沒有任何的構造函數new'ing起來,也沒有任何變數器),因此這兩個類從一開始就將實例的關系多重性實例化為“ 0”(即空引用),並且永遠不會為“ 1”。 IssueWithBankStuff的成員變量other從未設置,而IssueWithOtherStuff的成員變量iban也從未設置。
因此,實際上,這可以滿足您要求的“ 0”部分。 但是,對於“ 1”部分,您要么需要在某個位置更新成員變量,要么需要一些變量。 例如,用於在IssueWithBankStuff的“其他”成員變量增變(例如setOther(IssueWithOtherStuff其他){...})和突變在IssueWithOtherStuff的“IBAN”成員變量(例如setIban(IssueWithBankStuff IBAN){...} )。 在實例化后調用此類mutators將為您提供多重性的'1'部分,同時允許初始的'0'多重性,並且當然可以通過在某個點將null傳遞給mutator來重新設置為'0'多重性。
如果您希望始終保持1到1的關系(而不是0..1到0..1),則由於實例化中的循環引用,在Java中實現此關系的選項有限。 最簡單的方法是在另一個的構造函數中新建一個,如下所示:
public class A
{
private B theRefToB;
public A ()
{
theRefToB = new B (this);
}
}
public class B
{
private A theRefToA;
public B (A a)
{
theRefToA = a;
}
}
當然,這確實需要謹慎使用,並且需要很多內聯注釋,因為您可以輕松地直接創建一個B,傳入一個新的A(),最后得到兩個B。 您直接構造的B將是一個孤兒,沒有連接的A,而第二個B將在A的構造函數中創建。您可能能夠使用一些巧妙的作用域來減輕這種風險,在C#中,您可能會使用內部范圍可能會有所幫助。 我必須挖掘一下有關Java的書。 在一對一關系中要注意的一件事是,它們是明智的設計,通常意味着兩種類型實際上是相同的,或者至少可以以這種方式設計/編程。
我還注意到,您可能在這里遇到概念合並問題。 您在IssueWithOtherStuff中引用了一個名為Iban的類型,然后也將對 IssueWithBankStuff的引用也稱為“ iban”。 這很令人困惑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.