【问题标题】:ClassDiagramm 0..1 to 0..1 relation missing steps to convert into java code类图 0..1 到 0..1 关系缺少转换成java代码的步骤
【发布时间】:2019-01-30 21:11:18
【问题描述】:

我想通过 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



}

【问题讨论】:

  • 为强制关系 [1] 编写代码比为选项关系 [0..1] 编写代码要困难得多。您编写的代码是可选的,因为我可以创建 IssueWithOtherStuff 而将字段 iban 留空。
  • 这两个类都应该是可选的。 :)

标签: java class uml class-diagram multiplicity


【解决方案1】:

0..1 表示可选性。通常,在编写代码时,您会使用命名为 Null、None、Nil、Void、Optional 等的特定于语言的构造。不确定 Java,但我记得有一些类似的东西。

【讨论】:

    【解决方案2】:

    您的示例中的两个类(IssueWithBankStuffIssueWithOtherStuff)都没有相互引用 set(例如,没有构造函数任何一个类中的参数都带有对另一个的引用,构造函数中也没有任何新的,也没有任何修改器),这意味着两个类都将在“0”处使用关系多重性进行实例化(即null ref) 从一开始就永远不会是“1”。 IssueWithBankStuff 的成员变量 other 从未设置,IssueWithOtherStuff 的成员变量 iban 也从未设置。

    所以,实际上,这可以满足您要求的“0”部分。但是,对于 '1' 部分,您要么需要在某处更新成员变量,要么需要一些修改器。例如。 IssueWithBankStuff 中的“other”成员变量(例如 setOther(IssueWithOtherStuff other) { ... })和IssueWithOtherStuff(例如 setIban(IssueWithBankStuff iban) { ... })。在实例化后调用此类 mutators 将为您提供多重性的“1”部分,同时允许初始的“0”多重性,当然,通过在某个时候将 null 传递给 mutators 来设置回“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;
        }
    }
    

    这当然需要小心使用,并且需要大量的内联 cmets,因为您可以轻松地直接创建一个 B,传入一个新的 A(),最后得到两个 B。您直接构造的 B 将是一个孤儿,没有连接的 A,第二个 B 将在 A 的构造函数中创建。您可以使用一些巧妙的范围界定来减轻这种风险,在 C# 中您可能会使用internal 范围可能会有所帮助。我不得不翻出我关于 Java 的书。关于 1 对 1 关系需要注意的一点是,从设计角度来看,它们通常意味着这两种类型实际上是相同的,或者至少可以这样设计/编程。

    我还注意到,您在这里可能遇到概念混淆的问题。您有一个名为 Iban 的类型,它在 IssueWithOtherStuff 中被引用,然后您也将对 IssueWithBankStuff 的引用称为“iban”。这很令人困惑。

    【讨论】:

      【解决方案3】:

      一个用于管理A -0..1----0..1- B 的Java 示例,当一个集合在任一侧完成时,它也在另一侧完成:

      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));
        }
      }
      

      执行总是写true

      请因为我不是Java程序员,如果有些事情很愚蠢,请不要对我太粗鲁^^

      【讨论】:

        猜你喜欢
        • 2017-11-28
        • 1970-01-01
        • 1970-01-01
        • 2018-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-16
        相关资源
        最近更新 更多