Type Adapter


A TypeAdapter provides a uniform protocol for setting variables and calling methods of both primitive and object types. Java reflection abstracts these differently:

  • aField.setInt(anObject, anInt) -- for primitive type int.
  • aField.set(anObject, anInteger) -- for type Integer.

This is made uniform:

  • aTypeAdapter.set(anInteger) -- for int or Integer.

The type adapter also holds the object that is to be set.

View source.


Implementation Notes

TypeAdapter distinguishes between the target upon which it reflects and the fixture for whom it does the reflection. These objects are one in the same for column and action fixtures, but are distinct for row fixtures. A RowFixture produces arbitrary domain objects as rows. These become the targets of the TypeAdapters. However, the fixture may still participate in string parsing of domain types used by fields and methods of the domain object.

Subclasses of TypeAdapter may be specialized as needed to effect reads and writes. Java requires quite a number of subclasses as mentioned above. C# requires fewer subclasses since it's reflection protocol is more uniform. C++ could bypass the absence of a reflection protocol by constructing custom subclasses for every single variable.

One subclass of TypeAdapter handles arrays of other types. These adapters hold another adapter to parse and print the individual (comma separated) elements of the arrays.

TypeAdapter assumes that instances of Class are a suitable identification of type for parsing and printing strings. TypeAdapter's type field need not be an object at all so long as TypeAdapter or the fixtures to which it delegates parsing and formatting are able to switch to appropriate type conversions based on type.

WardOnTypeAdapters


See also: WeakTypeIssues

 

Last edited April 5, 2005
Return to WelcomeVisitors