Skip to end of metadata
Go to start of metadata

Interfaces for: (tick) create, read-only, and visiting - giving us transformations

The last safe option

This technique is the last "safe" option that is easy to get right. It offers the best balance of being safe for use in a multi-threaded environment, while still letting client code "do everything".

Story: Allow client code to transform existing content. The result will be a modified copy based on the original.

Create

(tick)

Factory used to copy

Read

(tick)

Read-only

Visit

(tick)

 

Write

(error)

 

Thread-safe

(tick)

Read-only interfaces are safe

Mixed Implementations

 

not required

Interfaces

Define a Read-only Interface

Define a Factory

Define a Visitor

Examples

Copy

It may not at first be obvious how the combination of Read-only interface, a Factory for creating new instances and a Visitor work together to allow transformations.

Define a visitor that uses a factory to copy the data structure - this is an "identity" Transform

Here is an example of how this can be used:

Example Transform

Client code can subclass the Identity Transform to just target the data structure they are interested in modifying

There one subtle consequence: Because the interfaces are read-only, if no transformation has been made then the original can still be used.

Here is how the above TemporalTransform can be used:

Labels
  • None