Component-Based Software Engineering

Document Sample
Component-Based Software Engineering Powered By Docstoc
					Object-Oriented Software Engineering


              CS288




                                       1
Multiple Classes


  Contents
  • Lists of objects
  • Vectors
  • Growing and shrinking Vectors
  • Iteration over Vector Object
  • Generic Objects




                                    2
Back to first version of SimpleClass
 public class SimpleClass {

     private String uselessField;

     public SimpleClass (String newFieldVal) {
       setUselessField (newFieldVal);
     }

     public String getUselessField () {
       return uselessField;
     }

     public void setUselessField (String newUselessField) {
       uselessField = newUselessField;
     }

     public static void main (String[ ] args) {
            // Add Code Here
     }
 }
                                                              3
The FooClass class
    public class FooClass {

        private String fieldOfFoo;

        public FooClass (String newField) {
          setFieldOfFoo(newField);
        }

        public String getFieldOfFoo ( ) {
          return fieldOfFoo;
        }

        public void setFieldOfFoo (String fieldOfFoo) {
          this.fieldOfFoo = fieldOfFoo;
        }

    }

                                                          4
Lists of Classes
        Add new field to SimpleClass

               private Vector listFoo = new Vector ( );

       The Vector class implements a growable array of objects.
        Vector V:


                    v0         v1         v2        v3




                    v0         v1         v2        v3



                                                                  5
Vectors have arbitrary size


        Vectors are declared with no fixed length

                private Vector listFoo = new Vector ( );

        Whereas an array must have a fixed size given
        at initialisation:

                private String[ ] stArray = new String[20];




                                                              6
Methods for adding and removing objects
  New methods for SimpleClass to add and remove objects from the
  listFoo field.



   public void addFoo (FooClass newFoo) {
     listFoo.addElement (newFoo);
   }
                                                     Vector class method
                                                     for increasing size of
                                                     a vector object and
   public void removeFoo (FooClass newFoo) {
                                                     inserting new object
     listFoo.remove(newFoo);
   }
                                                Vector class method
                                                for decreasing size of
                                                a vector object and
                                                removing object


                                                                              7
Vectors hold any object


      • Vectors can hold any objects, whereas an array has fixed types for
        the objects in the array.

      • Do not have to declare what type of objects will be stored in a
        vector when declaring them.

      • Java compiler warns you of possible problems when compiling
       code:
       warning: [unchecked] unchecked call to
       addElement(E) as a member of the raw type
       java.util.Vector
              listFoo.addElement (newFoo);
       1 warning




                                                                             8
Experiment with main method
public static void main (String[ ] args) {
    SimpleClass s1 = new SimpleClass("test object");
    FooClass f1 = new FooClass("apple");
    FooClass f2 = new FooClass("orange");
    FooClass f3 = new FooClass("pear");
    FooClass f4 = new FooClass("banana");
    FooClass f5 = new FooClass("grape");
    s1.addFoo (f1);
    s1.addFoo (f2);
    s1.addFoo (f3);
    s1.addFoo (f4);
    s1.addFoo (f5);
  }




                                                       9
2nd Experiment
public static void main (String[ ] args) {
    SimpleClass s1 = new SimpleClass("test object");
    FooClass f1 = new FooClass("apple");
    FooClass f2 = new FooClass("orange");
    FooClass f3 = new FooClass("pear");
    s1.addFoo (f1);                           s1 before final statement
    s1.addFoo (f2);
    s1.addFoo (f3);                                  s1 after final statement
    s1.removeFoo (f1);
  }




                                                                                10
3rd Experiment
public static void main (String[ ] args) {
   SimpleClass s1 = new SimpleClass("test object");
   FooClass f1 = new FooClass("apple");
     FooClass fx1 = new FooClass("apple");
     FooClass f2 = new FooClass("orange");
     FooClass f3 = new FooClass("pear");     s1 before and after
     s1.addFoo (f1);                         final statement.
     s1.addFoo (f2);
                                             f1 not removed
     s1.addFoo (f3);
     s1.removeFoo (fx1);
 }




                                                                   11
4th Experiment
public static void main (String[ ] args) {
    SimpleClass s1 = new SimpleClass("test object");
                                                     fx1 refers to same object as f1,
    FooClass f1 = new FooClass("apple");
                                                     and removing it means removing
    FooClass fx1 = f1;                               the object it refers to. Hence f1
    FooClass f2 = new FooClass("orange");
                                                     is removed from the Vector.
    FooClass f3 = new FooClass("pear");
    s1.addFoo (f1);
    s1.addFoo (f2);                            s1 before final statement
    s1.addFoo (f3);
                                                       s1 after final statement
    s1.removeFoo (fx1);
  }




                                                                                     12
Iteration with Vectors
   public static void main (String[ ] args) {
      SimpleClass s1 = new SimpleClass("test object");
      FooClass f1 = new FooClass("apple");
      FooClass f2 = new FooClass("orange");
      FooClass f3 = new FooClass("pear");
      s1.addFoo (f1);
      s1.addFoo (f2);
      s1.addFoo (f3);
      for (Enumeration e = s1.listFoo.elements () ; e.hasMoreElements () ;) {
          FooClass fx = (FooClass)e.nextElement ();
          System.out.println(fx.getFieldOfFoo ());
      }
   }

  • An Enumeration object e contains a set of objects that can be generated one
    at a time.
  • Note the generated objects are of type Object, and they have to be
    explicitly cast to the correct type in the loop.
  • s1.listFoo is a Vector object, and e = s1.listFoo.elements () returns an
    enumeration object for the Vector containing all the elements in the Vector.
                                                                                   13
Iteration in for loop
   for (Enumeration e = s1.listFoo.elements () ; e.hasMoreElements () ;) {
           FooClass fx = (FooClass)e.nextElement ();
           System.out.println(fx.getFieldOfFoo ());
   }
                                              On first iteration, e has this value.
                                              The integer count is used to keep
                                              track of which objects have been
                                              generated so far.




                                                                                      14
Iteration with standard for loop

    Iteration with Enumeration class:

         for (Enumeration e = ob1.listFoo.elements () ; e.hasMoreElements () ;)
             {
                 FooClass fx = (FooClass)e.nextElement ();
                 System.out.println(fx.getFieldOfFoo ());
               }

    Can also be achieved with a more standard style of for loop:

         for (int i =0 ; i < ob1.listFoo.size ( ) ; i++) {
                  FooClass fx = (FooClass)s1.listFoo.elementAt(i);
                  System.out.println (fx.getFieldOfFoo ());
               }




                                                                                  15
Iteration with variable placeholders


       To highlight the structure of the standard for loop we can
       introduce some placeholder variables:

          Vector vec = s1.listFoo;
          int size = vec.size( );
          for (int i =0 ; i < size ; i++) {
                    FooClass fx = (FooClass)vec.elementAt(i);
                    System.out.println (fx.getFieldOfFoo ());
                }

       The additional variables do not make the code ‘better’,
       they merely simplify the syntax so that the loop
       structure is easier to see.




                                                                    16
Making Vectors Safe


   • SimpleClass is not accessing Vector in safe manner.
   • Compiler is flagging problem:
     warning: [unchecked] unchecked call to
     addElement(E) as a member of the raw type
     java.util.Vector
        listFoo.addElement (newFoo);
     1 warning




                                                           17
Vectors are Raw Types

    When we declare a new variable as a Vector object we do
    not declare what type of objects will be stored in the Vector:
    private Vector listFoo = new Vector ( );


    To see why this can be a problem, change the field listFoo to
    be public:
    public Vector listFoo = new Vector ( );

    Change main method for SimpleClass to
     public static void main (String[ ] args) {
         SimpleClass s1 = new SimpleClass ("test object");
         s1.listFoo.addElement ("Flip Flop");
         /* other code */
    }

                                                                     18
Vectors are Raw Types


     • Despite the fact that the listFoo Vector is only ‘meant’ to store
       FooClass objects we have added a String object.
       This does not cause a compiler error, however at run time we
       get this error:
     • Exception in thread "main"
       java.lang.ClassCastException: java.lang.String
       at vectorexample.SimpleClass.main
       (SimpleClass.java:42) Java Result: 1




                                                                           19
Generics
     • A class that can contain classes of arbitrary type, without having to
       explicitly declare what the types are, is known as a generic class.
     • Java 1.5 provides a mechanism for telling the compiler what type of
       object is meant to be stored in a generic class.
     • In our example we would use this syntax to instruct the compiler that
       only FooClass objects are allowed in the listFoo Vector.

         public Vector<FooClass> listFoo = new Vector <FooClass> ( );




The compiler will now pick up on our attempt to add a String object to listFoo:




                                                                                  20
Building Class Example


   As an elementary example consider a
   class to represent the concept of a
   building.

   Buildings have rooms.

   We will need a Room class.
   Rooms have a size (floor area), doors
   and windows.

   As a starting point we will think of a
   Building as a class that contains a
   Vector of rooms.



                                            21
Initial Room Class
 /* start of class */     /* then methods */
 public class Room {      public Double getArea() {return area; }

   public Room() {        public void setArea(Double area) {
   }                           this.area = area;}

   /* fields */           public int getDoors() { return doors;}
   private Double area;
   private int doors;     public void setDoors(int doors) {
   private int windows;        this.doors = doors;}

                          public int getWindows() {return windows;}

                          public void setWindows(int windows) {
                                this.windows = windows;}
                          } /* end of class */


                                                                      22
Extra methods for Room class


 /* extra constructor */
    public Room(Double newArea, int newDoors, int newWindows) {
      setArea(newArea);
      setDoors(newDoors);
      setWindows(newWindows);
    }

 /* Later we want to display room data in tabular form,
    this will be useful for that */
    public String[ ] toStringArray() {
       String areaSt = area.toString();
       String doorsSt = Integer.toString(doors);
       String windowsSt = Integer.toString(windows);
       String[ ] room_fields = {areaSt, doorsSt, windowsSt};
       return room_fields;
    }
                                                                  23
Initial Building Class

      public class Building {

          public Building() {
             rooms = new Vector<Room>();
          }                                  Note Vector is constrained
          private Vector<Room> rooms;        to be of type Room

          public void setRoom(Vector<Room> newRooms) {
            rooms = newRooms;
          }
          public void addRoom(Room newRoom) {
            rooms.addElement(newRoom);
          }

          public Vector getRooms() {
            return rooms;
          }
      }
                                                                          24
Presenting Rooms Data

      Given a building where we have lots of rooms it may be
      useful to be able to gather all the relevant data for those
      rooms into a single table (array of arrays).

      It should not be the job of the Building class to display
      that data in any particular form. It is reasonable to suppose
      the Building class should extract the relevant data from
      the rooms Vector and present it as an array of arrays.

      How do we do that?

      Need a new method:

               public String[ ][ ] roomsToArrayArray() {
                   /* code goes here */
              }

                                                                      25
Presenting Rooms Data

     public String[ ][ ] roomsToArrayArray() {
       Room rm;
       int rooms_length = rooms.size();
       String[ ][ ] return_array = new String[rooms_length][3];

         for (int i = 0; i < rooms_length; i++) {
            rm = rooms.elementAt(i);
            return_array[i] = rm.toStringArray();
         }
         return return_array;
     }
                                           Note, its not up to the Building
                                           class to decide how a room is
                                           converted to an array.
                                           That job is done by the Room
                                           object
                                                                              26
Rooms Values (as shown in NetBeans Debugger)




                                               27
Output for roomsToArrayArray




                               28
Summing Up

 This lecture discussed


 • Storing objects within a list structure.

 • The Vector class

 • Adding and deleting elements from a Vector object.

 • Iteration over a Vector object.

 • Problems with raw types, and safely using generic classes.




                                                                29