Practice w23hm3q1
Midterm 3 Question 1 (March 29) -- Hindle
Submit a PDF, PNG or JPEG
5 marks
You must add your CCID to your answer
Adapter and Sequence Diagram
You’ve written an object adapter (RTFAdapter) to adapt rich text files to an interface compatible with plain text files. Show your adaptation via a UML sequence diagram. Remember to include all the actors, components, lifelines, iteration, and activation boxes. Use good names for the methods.
We want to read the lines of a RichTextFile (RTF), but RichTextFile—which we got from a 3rd party library—doesn’t implement the same interface. You’ve implemented an object adapter so we can use the ReadOutLoud class to read out loud (readText) a RichTextFile.
- Show a sequence diagram of Driver.run() operating with an object adapter (RTFAdapter) that adapts a RichTextFile to a TextLineReader.
- RTFAdapter is an object adapter
interface TextLineReader {
// while ! isEndOfile() you can call readNextLine
public boolean isEndOfFile();
// move forward 1 line of text, read it, and return it.
public String readNextLine();
}
class PlainTextFile implements TextLineReader {
public PlainTextFile(String file) { /* ... */ }
public boolean isEndOfFile() { /* ... */ }
public String readNextLine() { /* ... /; }
/ ... /
}
class RichTextFile {
public RichTextFile(String file) { / ... */ }
// How many lines in the RTF
public int totalLines() { /* ... */ }
// Return a specific line
public String readLine(int i) { /* .../ }
/ ... /
}
class ReadOutLoud {
public void readText(TextLineReader r) {
while( ! r.isEndOfFile() ) {
sayTheLine( r.readNextLine() );
}
}
public void sayTheLine( String s) { / ... / }
/ ... /
}
class RTFAdapter implements TextLineReader {
RTFAdapter(RichTextFile rtf) { / ... / }
public boolean isEndOfFile() { / ... / }
public String readNextLine() { / ... / }
/ ... */
}
public class Driver {
public static void run() {
new ReadOutLoud().readText(
new RTFAdapter(
new RichTextFile("text.rtf")
)
);
}
}