2

I want to write java code by an uml/class-diagram. But I'm not sure about it or how to write it for the relation 0..1 to 0..1

I didn't find any information about this relation.

1 to 0..1 is possible and I know how to create it. Here is my class diagram with relation 0..1 to 0..1:

enter image description here

I wrote this code for it.

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



}
qwerty_so
  • 35,448
  • 8
  • 62
  • 86
speedyG
  • 33
  • 9
  • It is much harder to write code for a mandatory relation [1] than it is for an option relation [0..1]. The code you wrote is for optional as I can create `IssueWithOtherStuff` while leaving the field `iban` empty. – Geert Bellekens Jan 31 '19 at 07:44
  • both classes should be optional. :) – speedyG Jan 31 '19 at 14:01

3 Answers3

0

0..1 means optionality. Generally when going to code you would use language specific constructs that are named Null, None, Nil, Void, Optional, etc. Not sure about Java, but I remember there's something along those lines.

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
0

An example in Java to manage for A -0..1----0..1- B, when a set is done on any of the two sides it is also done on the other side :

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

And to check 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));
  }
}

The execution writes true all the times

Please because I am not a Java programmer don't be too rude with me if something is stupid ^^

bruno
  • 32,421
  • 7
  • 25
  • 37
0

Neither of the two classes in your sample (IssueWithBankStuff or IssueWithOtherStuff) ever have their references to each other set (e.g. there's no constructor parameter in either class that carries a reference to the other, nor is there any new'ing up in the constructors, nor are there any mutators for them) so that means both classes would be instantiated with the relationship multiplicity at '0' (i.e. null ref) from the get go and could never be '1'. IssueWithBankStuff has member variable other which is never set, and IssueWithOtherStuff has member variable iban which is also never set.

So, actually, that takes care of the '0' part of your requirement. However, for the '1' part you either need to new up the member variables somewhere or you need some mutators for them. E.g. a mutator for the 'other' member variable in IssueWithBankStuff (e.g. setOther(IssueWithOtherStuff other) { ... }) and a mutator for the 'iban' member variable in the IssueWithOtherStuff (e.g. setIban(IssueWithBankStuff iban) { ... }). Calling such mutators post-instantiation would give you the '1' part of the multiplicity whilst allowing the initial '0' multiplicity and, of course, setting back to '0' multiplicity by passing null into the mutators at some point.

If you wanted to have the relation as always 1 to 1 (not 0..1 to 0..1), then there are limited options to achieve this in Java due to circular references in instantiation. The simplest way would be to new up one inside the constructor of the other, something like this:

public class A
{
    private B theRefToB;

    public A ()
    {
        theRefToB = new B (this);
    }
}

public class B
{
    private A theRefToA;

    public B (A a)
    {
        theRefToA = a;
    }
}

This does of course require care in use, and plenty of in-line comments, because you could easily create a B directly, passing in a new A (), and ending up with two Bs. The B you construct directly would be an orphan, with no connected A, and the second B would be created inside the constructor of the A. You might be able to use some clever scoping to mitigate this risk, in C# you'd use possibly the internal scope which might help. I'd have to dig out my books on Java. One thing to note on 1 to 1 relationships is that they -- design wise -- usually mean the two types are actually one and the same, or at least can be designed/programmed that way.

I note also that you may have an issue here with concept conflation. You have a type called Iban which is referenced in IssueWithOtherStuff, you then also call the reference to IssueWithBankStuff 'iban' as well. This is quite confusing.

muszeo
  • 2,312
  • 9
  • 13