8 Replies Latest reply on Apr 22, 2010 8:30 PM by 800387

    Question about invariants

    843853
      Hello I hope this is in the right section. I have a Warehouse class defined as the following:

      public class Warehouse implements Serializable{
      private TreeSet<Product> produse=new TreeSet<Product>(); nothing more

      I am required to write an invariant for this class in the form of isWellFormed (it is for an assignment in class). The class Product already has an invariant isWellFormed defined. My question is what should I write in the invariant for the Warehouse? ( is an invariant really necessary or is it just forcing it ?).
        • 1. Re: Question about invariants
          800387
          An invariant is a boolean expression that if true at the beginning of a method invocation remains true at the end of a method invocation as well. Another take on this (in Contrac4J for example) is that an invariable must always remain true after an object is constructed. In most design-by-contract implementations, invariants are specified at the class or field level. [http://www.contract4j.org/contract4j/c4j5#how]

          So, I am not sure what you are trying to accomplish here. Normally, you would use AOP to check the invariant at the start and completion of each method. Or, you could do it the manual way and in each method call perform the same check as the first and last statement of the method.

          - Saish
          • 2. Re: Question about invariants
            843853
            Thank-you for the fast reply. What I'm asking is what should I check for if I am going to write an invariant for the class (and it's methods) Warehouse. Because Product already has an invariant defined for it and I thought that maybe Warehouse doesn't need one ( because it is composed of products and if each product is well formed then I thought that it doesn't need one).
            • 3. Re: Question about invariants
              jschellSomeoneStoleMyAlias
              Roronoa_Zorro wrote:
              The class Product already has an invariant isWellFormed defined. My question is what should I write in the invariant for the Warehouse? ( is an invariant really necessary or is it just forcing it ?).
              Enumerate the collection validating the value of each item in the collection.
              • 4. Re: Question about invariants
                800387
                An example of an invariant for a collection might be: the size is greater than zero, the size is less than ten, is never null, etc.

                - Saish
                • 5. Re: Question about invariants
                  843853
                  Thank you so very much for the replies. Any other advice is welcomed, because of the fact that the Product class also contains an invariant I can only check if the collection is either null or if the size of the collection is between certain limits.
                  • 6. Re: Question about invariants
                    800387
                    Roronoa_Zorro wrote:
                    Thank you so very much for the replies. Any other advice is welcomed, because of the fact that the Product class also contains an invariant I can only check if the collection is either null or if the size of the collection is between certain limits.
                    That is not so much the reason as general OO design principles. The invariant should really only check its own class's instance variables. If you find yourself writing an invariant in one class for another class, you have likely violated encapsulation in OO. (That and checking private variables would be impossible.)

                    - Saish
                    • 7. Re: Question about invariants
                      843853
                      OK , thank you for your help in this matter. Some aspects have become clearer .
                      • 8. Re: Question about invariants
                        800387
                        You are welcome. Best of luck.

                        - Saish