Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

This homework can be done using either the Elementary or Intermediate language level of DrJava. If you are comfortable with casting between primitive types, the Intermediate level is probably a better choice because it supports such casting operations. You can test these operations in the Interactions pane, which supports the full Java language regardless of what language level is selected.

Download and extract the attached file shapes.zip (Intermediate version: shapes1.zip) into the same subdirectory. This .zip file contains the following files:

  • Shape.dj0/dj1
  • Rectangle.dj0/dj1
  • Circle.dj0/dj1
  • TestRectangle.dj0/dj1
  • TestCircle.dj0/dj1

Use DrJava to open all these files and perform a Compile All (by clicking the Compile button). Click the Test button to run the two JUnit test files. Everything should compile and pass all the JUnit tests. Use these files as examples for documentation, coding style and JUnit testing.

Composite Design Pattern for List

...

Code Block
/** Abstract list structure.  IntList := EmptyIntList + ConsIntList(int, IntList) */
abstract class IntList { }
 
/** Concrete empty list structure containing nothing. */
class EmptyIntList extends IntList { }
 
/** Concrete non-empty list structure containing an int, called first,
 and *an andIntList acalled rest, which is a list structure.
  */
class ConsIntList extends IntList {
    int first;
    IntList rest;
}

The above implementation is an example of what is called the Composite Design Pattern. The composite design pattern is a structural pattern that prescribes how to build a container object that is composed of other objects whose structure is isomorphic to that of the container itselfspecial case of the union pattern where one or more of the variants for the union type T contains fields of root type T. In this pattern, the container union is called a composite. Here the container object union type is IntList and the list and variant ConsIntList is said to be a composite : because it is a list and is composed of a substructure that is itself a listincludes a field of type IntList.

The composite pattern also prescribes a coding pattern for the container's its methods: when a container variant is called to perform an operation, it traverses through its list fields of composed objects root type and call calls on them to perform the same operation. It allows the a client to treat the container and what it contains uniformly by making use of an instance of type T and it embedded instances uniformly using polymorphism.

This coding pattern is called the interpreter design pattern: we are interpreting it interprets the abstract behavior of a class in each of the its concrete subclasses. The composite pattern is a pattern refers to express the the structure of a systemthe composite type hierarchy, while the interpreter pattern is used to express the behaviors (i.e. methods) of the systemrefers to how the behavior of the variants of the type are defined uniformly via polymorphism.

Interpreter Design Pattern for List

The interpreter design pattern applied to the above composite list structure prescribes a coding pattern for list operations that is analogous to Scheme function template. It entails declaring an abstract method for each list operation in the abstract list class, IntList, and defining corresponding concrete methods in the concrete list subclasses: the empty list class, EmptyIntList}, and the non-empty class, {{ConsIntList. The concrete method for EmptyIntList corresponds to the base case in the Scheme function template while the concrete method in ConstIntList corresponds to the recursive case by calling the same method on its rest.

The following is the coding template for the interpreter design pattern for IntList and its subclasses.

...