INFORMATION & COMPUTER SCIENCE DEPARTMENT, KFUPM
ICS202 : DATA STRUCTURES
LAB #01: Introduction to Design Patterns
To gain experience with:
· The Container Design Pattern
· The Iterator Design Pattern
· The Visitor Design Pattern
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
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.
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.
(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.
(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.
(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.
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.