In Replacers and strategies as first class objects describes the main advantages of having strategies as first class objects. This section presents an example that shows how to use strategies. Although the example presented shows a dumping strategy, the same reasoning applies to loading strategies.
NewDumpingStrategy implements
dumpingReplacementFor:for:, which is part of the API used by
Replacer to request replacement of an object based on its class.
Now, when the ObjectDumper in the above example is used, the new message
newDumpingReplacementForReplacer: is sent from the strategy to the target object (object to be dumped). This message must be defined in
Object. The following example shows an obvious implementation.
You do not need to call the newDumpingReplacementForReplacer: message. When implementing a new strategy, you can choose whatever name you like for the new message. The set of messages the replacers and strategies understand is part of the public framework API. The messages the strategies use to interact with target objects is defined by the application.
The code above combined with the new strategy used in an ObjectDumper (see "Example: Plugging new dumping strategy in ObjectDumper" on page
***) replaces all instances of
CompiledMethod with a
CompiledMethodDescriptor that carries the class name (a
String) where the
CompiledMethod was compiled and the selector itself (a
String as well).
Using the loading replacement for CompiledMethodDescriptor will ensure that a descriptor for a compiled method is properly mapped back to the local
CompiledMethod in the image. The example code swaps only the minimum information necessary, just enough to rebind the object to an equivalent object in the image where it will load.
Copyright 2005, 2019 Instantiations, Inc. All rights reserved.