CSC 308 Lecture Notes Weeks 9 and 10
Modeling Idioms
Requirements for File and Edit Commands
Non-Functional Requirements
import java.util.Collection; /** * A server has a list of authentic users and some data. */ abstract class Server { Collection<UserRecord> users; Data data; /** * Login to this server to access its data. * pre: exists (UserRecord user ; users.contains(user) ; user.id.equals(id) && user.password.equals(password)); post: return.equals(data); */ abstract Data login(String id, String password); } /** * A UserRecord is an ID/Password pair. A more elaborate */ abstract class UserRecord { String id; String password; } /** * Data is a purely abstract class that represents the * data stored on a server that's accessible via login. */ abstract class Data {}
import java.util.List; /** * A sequence of items index from 1 to items.size(). The */ abstract class Sequence { List<Item> items; int cur_item; /** * Move to the next item in the sequence if not at the * last item. pre: cur_item < items.size(); post: cur_item' == cur_item + 1; */ abstract void next(); /** * Move to the previous item in the sequence if not at * the first item. pre: cur_item > 1; post: cur_item' == cur_item - 1; */ abstract void prev(); } /** * An item in a sequence. */ abstract class Item {}
import java.util.List; /** * HTML content consists of command tags and text. */ abstract class HtmlContent { List<TagAndValue> content; /** * Render this into the format displayed to a user. * See the <a href= * "http://www.w3.org/TR/html5/semantics.html"> W3C * </a> the for the full specification of how this * rendering is performed. */ abstract RenderedHTMLContent display(); } /** * Class TagAndValue has an enumerated tag value and * string value. */ class TagAndValue { Tag tag; String value; } /** * The Tag enumeration consists of the tags that need to * be modeled for a particular tool's use of HTML. */ enum Tag { TEXT, TAG1, TAG2, /* ... */ } /** * As defined by W3C at <a href= * "http://www.w3.org/TR/html5/semantics.html".> */ abstract class RenderedHTMLContent {}
import java.util.Collection; /** * A generic spreadsheet consists of rows. */ abstract class SpreadSheet { Collection<SpreadSheetRow> rows; } /** * A row has a number and a collection of columns. */ abstract class SpreadSheetRow { int number; Collection <Column> columns; } /** * A column has a heading, some data, and possibly * subcolumns. When subcolumns are non-nil, the data is * typically some combination of the subcolumn data * values, such as the sum if data values are numeric. */ abstract class Column { String heading; Data data; Collection<Column> subColumns; } /** * Whatever kind of data can appear in spreadsheet cells. */ abstract class Data { /* ... */ }
/** * Class Wizard has operations that must be performed in * sequential steps. To enforce this sequential behavior, * each wizard step takes a unique type of input and * produces a unique type of output. * * * When the types of the i/o objects are unique, then the * requirement that wizard step N must follow step N-1 is * enforced by operation stepN being the only operation * that accepts an input of type StepMinus1Ouput * * * If output types themselves are not unique type, a * postcondition on stepNMinus1 can set a component to a * unique value that a precondition on stepN checks, to * ensure the data in fact come from step 1. * */ abstract class Wizard { /** * Perform wizard step N, producing output to be given * to wizard step 2. */ abstract Step1Output step1(Step1Input in); /** * Like step1. */ abstract Step2Output step2(Step1Output in); /** * Like steps 1 through N-1. */ abstract StepNOutput stepN(StepNminus1Output in); } abstract class Step1Input { /* ... */ } abstract class Step2Input { /* ... */ } abstract class StepNInput { /* ... */ } abstract class Step1Output { /* ... */ } abstract class Step2Output { /* ... */ } abstract class StepNminus1Output { /* ... */ } abstract class StepNOutput { /* ... */ }
import java.util.List; /** * The recursive definition of the SumList function uses the following * strategy: * * (a) If the list contains no elements, then the sum is 0 * (b) Otherwise, recursively compute the sum of the first list element * with the sum of the rest of the elements * * The "rest of" a list is denoted by the expression * * l[2:#l] * * The colon-separated expression between the square brackets denotes a * sublist of the form * * start:end * * where 'start' and 'end' are the index positions of the sublist. The '#' * operator is list length. Hence, the sublist [2:#l] extends from the second * position of the list to the last position. I.e., it's the "rest of" a list * beyond position 1. * */ class IntList { List<Integer> data; int sum(List<Integer> data) { if (data.size() == 0) return 0; else return data.get(0) + sum(data.subList(2, data.size() - 1)); } }
/** * Simplified data model for a tool workspace. */ abstract class ToolWorkspace { ToolData data; ToolData undo_data; ToolData redo_data; /** * To perform some tool operation, set the data of the * output workspace to the new data, and the undo data * of the output to the original data in the input * workspace. post: undo_data'.equals(data) && data'.equals(new_data) && redo_data' == null; */ abstract void some_operation(ToolData new_data); /** * If some tool operation has been performed that sets * undo data in the workspace, then the effect of undo * is to set workspace data to that undo data, * otherwise undo has no effect. pre: undo_data != null; post: data'.equals(undo_data) && undo_data' == null && redo_data'.equals(data); */ abstract void undo(); } /** * Whatever data for which operations are undoable. */ abstract class ToolData { /* ... */ }
End of Idiom Discussion; on now to Non-Functional Requirements