27.07.2013 Views

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

248 <strong>Model</strong>ing Objects<br />

static class Contract<br />

{<br />

static Map salary = Map.EmptyMap;<br />

// ...<br />

}<br />

}<br />

In this example each employee has a salary. There is a state variable called salary<br />

that maps Employee values to integers that represent an employee’s pay rate. The<br />

value Employee(1) might be associated with the salary 200. The salary field is an<br />

example of a field map, or table that relates an object identifier to an associated data<br />

value. A field map is a way of encoding in a single state variable elements of state<br />

that occur on a per-instance basis. Note that the salary field itself is global (static<br />

in C# ).<br />

Introducing an identifier class like Employee is a modeling idiom that allows us<br />

to build up arbitrary object graphs, with independently updated fields. For example,<br />

we use a field map to treat salary as a separate element of state. The type Employee<br />

provides keys for the salary field map. The Employee class contains only fields that<br />

establish identity. Other kinds of data are excluded.<br />

The field map idiom is especially useful when we want to encode object graphs<br />

with possibly circular references. For example, an employee might have a “backup<br />

contact” who handles work in the employee’s absence. Backup contacts might<br />

introduce circular references in the object graph. (A has B as backup contact and<br />

vice versa.)<br />

Note that it wouldn’t be possible to add a “backup contact” field to a compound<br />

value type like the Employee class. You may recall from Section 10.3 that compound<br />

values like Employee are immutable types with structural equality. Circular<br />

references among fields of such types are not possible.<br />

Since the idiom of independently updated per-instance fields occurs frequently,<br />

object-oriented languages like C# provide built-in support for instance fields. The<br />

N<strong>Model</strong> framework supports instance fields with a special class that implements<br />

field maps in the background. This class is called LabeledInstance. You must<br />

use this class (or a subclass) to program with objects in N<strong>Model</strong>. Here is an<br />

example:<br />

namespace Payroll2<br />

{<br />

class Employee : LabeledInstance<br />

{<br />

int salary;<br />

more free ebooks download links at:<br />

http://www.ebook-x.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!