This is an English version of Objektstrukturer - Partner-oppgave, and is concerned with the consistency of a 1-1 association between two partners (gender neutral marriage), i.e. ensuring that Partner objects are pairwise linked to each other.

An important part of the implementation of associations is to ensure consistency, i.e. that objects are correctly linked to each other in both directions. An example is a 1-1 association for partnership (gender neutral marriage), where two partners are linked when partnership is established and unlinked if divorced. In this programming task a Partner class must implemented that correctly handles the cases illustrated below.

The Partner class must support information about name (a String), which must be provided in the constructor and not be settable later, and partner, which is some other Partner object. The Partner class must have the following methods for reading the state:

  • getName() - returns the name of this Partner object
  • getPartner() - returns the Partner object related to this Partner object, or null, if no partnership has been established

The Partner class has only one mutation method, setPartner(Partner), which is used for both establishing a partnership, when the argument is a Partner object, and tearing it down, when the argument is null. The figures below illustrated the cases that must be handled, and which are tested by our tests.

#p1: Partner#p2: Partner

 

 

p1.setPartner(p2)

#p1: Partner#p2: PartnerpartnerpartnerEstablishing partnership: The Partner objects p1 and p2 are linked using a single call to setPartner. Before the call p1 and p2 are not linked, while after the call they are.
#p1: Partner#p2: Partnerpartnerpartner 

 

 

p1.setPartner(null)
#p1: Partner#p2: PartnerTearing down partnership: The Partner objects p1 are p2 unlinked using a single call to setPartner with null as argument. Before the call p1 and p2 are linked, while after the call they are not.
#p1: Partner#p2: Partner#p3: Partner#p4: Partnerpartnerpartnerpartnerpartner

 

 

p1.setPartner(p3)

#p1: Partner#p2: Partner#p3: Partner#p4: PartnerpartnerpartnerTearing down and establishing partnership: The Partner objects p1, p2, p3 and p4 are pairwise linked, before a single call to setPartner links p1 and p3, while p2 and p4 are unlinked from their respective partners.

The Exercise panel

Use the Exercise panel, open the Partner.ex file (tests > objectstructures Partner.ex) in the panel before you start programming.

The task

The task is (simply) to implement the Partner class and check (e.g. with a main method) that the Partner objects behave as specified/expected.

Test code for this task can be found here: objectstructures/PartnerTest.java. The original jextest code can be found here: objectstructures/Partner.jextest

 

  • No labels