1

I know what abstraction function and representation invariant are, but I have difficulty writing them on my own. Abstraction Function: A function from an object's concrete representation to the abstract value it represents. Representation Invariant: A condition that must be true over all valid concrete representations of a class. For example:

class Appointment{
    /**
     * AF: 
     * IR: 
     */
    private Time time;
    private Intervention intervention;
    private Room room;


    /** EFFECT initializes to null an appointment
     *  @param time REQUIRE != null
     *  @param intervention  REQUIRE != null
     *  @param room REQUIRE != null
     */
    public Appointment(Time time, Intervention intervention, Room room){
        time = null;
        intervention = null;
        room = null;
    }}

my question is: how could they be written?

Thank you.

cieco1109
  • 73
  • 6

1 Answers1

0

This way you force the implementator of classes that extends abstract A to define its own invariant.

abstract class A {
   public void doSth(){
       Invariant invariant = getInvariant();
       check(invariant);
       //do some work
       check(invariant);
   }

   //define your invariant in concrete impl
   protected abstract Invariant getInvariant();
}

I re-re-read your question again but i am still not sure. Or do you want to define invariant in the abstract class and check it in conrete implementations?

abstract class A {
   private void checkInvariant(){
       //check state
       //if state is breaking invariant throw exception
   }

   public void doSth() {
        checkInvariant();
        doActualWork();
        checkInvariant();
   }

   protected abstract void doActualWork();
}
Rob
  • 708
  • 8
  • 27