INFORMATION & COMPUTER SCIENCE DEPARTMENT, KFUPM

ICS202 : DATA STRUCTURES

LAB  #01: Introduction to Design Patterns


Objectives:

To gain experience with:

         The Container Design Pattern

         The Iterator Design Pattern

         The Visitor Design Pattern

 

1.  Downloads and Review:

Download the file lab01.zip from the lab document section of WebCT into your z-drive and unzip it.  After unzipping the file, a folder ics202 will automatically be created with the following files, most of which were discussed in lectures 3 and 4.

         Container.java

         AbstractContainer.java

         Visitor.java

         AbstractVisitor.java

         Iterator.java

               MethodNotImplemented.java
      
         PrintingVisitor.java

The code in these files should be understood from the lectures.  Open each of these files and review them to make sure you are familiar with what each of them does.  The rest of the lab (and indeed the rest of the course) depends very much these files.

 

2.  Examples of Container, Visitor and Iterator design patterns:

 

You would notice that in addition to the above files, the unzipping program also created a sub-folder, lab01 under the ics202 main folder and stored a number of files there.  These are examples of concrete implementations of Container, Visitor and Iterator design patterns and how to use them.

 

Example 1:  The class MyContainer is a concrete class that extends the AbstractContainer class.  It stores its data using a Comparable array.  Notice that since array size is fixed, we need to override the isFull() method inherited from the AbstractContainer class.  Notice also that since we are not interested in comparing two MyContainer containers, we are simply throwing MethodNotImplemented exception in the implementation of the compareTo method.

 

The test class, TestMyContainer creates an instance of MyContainer and adds ten Integer objects into it.  It then makes use of the toString() method inherited from the AbstractContainer class to print the contents of the container.

 

Example 2: The classes PrintingVisitor.java and TestPrintingVisitor.java show an implementation and use of a visitor.

 

Example 3: The class TestIterator.java shows how the elements in a container can be processed using an Iterator.

 

Example 4: The class AdditionVisitor.java is another implementation of a visitor showing that a visitor could have additional methods and instance variables if necessary.  The class TestAdditionVisitor shows how the AdditionVisitor may be used. 

 

Example 5: The class TestAdditionIterator shows how the elements of a container may be summed up using an Iterator object.

Example 6: The visit method of any visitor does not have a loop to iterate the objects of the visited container; this loop is in the accept method of the container. However, if one or more instance variables of an object of the visited container is a container or a data structure such as an array or a linked list, then the visit method may contain a loop or loops to iterate the objects of the instance variable(s). The given Student class contains a one-dimensional array instance variable  qzGrade. The visit method of StudentVisitor has a loop that iterates the elements of this array for each passed Student object. The class TestStudentVisitor shows how the StudentVisitor may be used.


3.  Task #1

(a)   Implement a visitor class SpecialVisitor, in the ics202.lab01 package, that has a constructor with the header: public SpecialVisitor(Comparable target, int n). The visitor can be used to determine whether or not a MyContainer instance has at least n target objects. Your visitor must be efficient; it must stop visiting the container immediately it determines there are at least n target objects in the container.

       Note: 

(b)  Write an application, TestSpecialVisitor, in the ics202.lab01 package, that creates an instance of MyContainer, reads 12 integer values from the user and inserts them, as Integer objects, into the container.  Your application should then prompt for an integer target, and an integer n. It should then:

          (i) determine whether or not the container contains at least n Integer target objects by using

               a SpecialVisitor instance and by displaying an appropriate message. 

          (ii) print all the contents of the container using an Iterator object.

 

4.  Task #2

(a)  Implement a visitor class MaxMinVisitor, in the ics202.lab01 package, that can be used to find the maximum and the minimum elements in a container of Comparable objects.  The MaxMinVisitor should have two additional methods, getMax( ) and getMin( ) that can be called after the visitor is done with its visit, to get the maximum value and the minimum values respectively.  NOTE:  Your MaxMinVisitor must be general.  That is, it must work with a MyContainer containing  any set of Comparable objects. Also all instance variables in MaxMinVisitor must be private.

 

(b)  Write a test program TestMaxMinVisitor, in the ics202.lab01 package, that adds some Double objects into an instance of MyContainer and then it uses the MaxMinVisitor to print the maximum and minimum elements of the container.

 

5.  Task #3

Save the file, TestPrintingVisitor.java as TestPrintingVisitor2.java and modify it such that instead of using PrintingVisitor, it creates an anonymous inner class that does exactly the same job as the PrintingVisitor and then use it to print the contents of the container.