|
| ||
|
|
Start of topic | Skip to actions
Homework 07 (Due 11:00am Friday, Feb 27, 2009)Submit via Owl-SpacePreliminariesThis 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 or shapes1.zip) into the same subdirectory. The files are available in Elementary and Intermediate form. It contains the following files:
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 ListThe following is an object-oriented formulation of lists of integers.
/** Abstract list structure. */
abstract class IntList {
}
/** Concrete empty list structure containing nothing. */
class EmptyIntList extends IntList {
}
/** Concrete non-empty list structure containing an int, called first,
* and a 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 itself. In this pattern, the container is called a composite.
Here the container object is the list and ConsIntList is said to be a composite: it is a list and is composed of a substructure that is itself a list.
The composite pattern also prescribes a coding pattern for the container's methods: when a container is called to perform an operation, it traverses through its list of composed objects and call on them to perform the same operation. It allows the client to treat the container and what it contains uniformly by making use of polymorphism.
This coding pattern is called the interpreter design pattern: we are interpreting the abstract behavior of a class in each of the concrete subclasses. The composite pattern is a pattern to express the structure of a system, while the interpreter pattern is used to express the behaviors (i.e. methods) of the system.
Interpreter Design Pattern for ListThe 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.
abstract class IntList {
abstract returnType methodName(parameter_list); // returnType may be void
}
class EmptyIntList extends IntList {
returnType methodName(parameter_list) {
// base case code
}
}
class ConsIntList extends IntList {
int first;
IntList rest;
returnType methodName(parameter_list) {
// ... first ...
// ... rest.methodName(parameter_list) ...
}
}
ProblemsApply the interpreter design pattern toIntList and its subclasses to write the following methods. Also write a JUnit test class to test all methods in EmptyIntList and a different JUnit test class to test all methods in ConsIntList. We strongly recommend that you write Template Instantiations as an intermediate step in developing your code BUT DO NOT submit these Template Instantiations (or corresponding Templates) as part of your code documentation. Confine your documentation to writing contracts (purpose statements in HTDP terminology) using javadoc notation. The javadoc documentation style will be discussed in the upcoming lab.
Topic Actions: Edit | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r13 < r12 < r11 < r10 < r9 | More topic actions
Webs: Main | TWiki | Africa | CPSX | EmbeddedSystems | Gpce | Houston | International | K12 | MetaOCaml | MulticoreOCR | ProgrammingLanguages | RAP | RIDL | Sandbox | SpeechClub | Teaching | Texbot | WG211 Web Actions: | |
This work is licensed under a Creative Commons Attribution 2.5 License. Please follow our citation guidelines.