Change implementation / class at runtime

I am looking for real world (open source) examples (or algorithms) that modify a specific class of an object (or variable) at runtime.

An example of this behavior in Java might look like a snapshot of the code below. Here, LinkedList , which works well in the context of frequent insertions and / or deletions, changes to ArrayList , which works well in the context of random access and iteration.

 List myList = new LinkedList(); /* Lots of inserts */ ... myList = new ArrayList( myList ); // 'change' into different class /* Lots of iteration */ ... 

The above Java example changes between LinkedList and ArrayList for the sake of performance.

However, examples in any language, for any data structure, using any technique * and for any reason are welcome.

* Technique: simple and simple, as in the example above, or using become: in SmallTalk or __class__ in Python or ...

+6
source share
4 answers

You might want to check options for using the become method in Smalltalk. The method changes the class of the instance at run time (or to change all references to the instance to refer to another instance)

Become commonly used to grow / shrink collections, for example. Dictionary with a lot of buckets, ByteArray with a large buffer, etc. Conversion from SmallInteger to BigIntegers is possible (the former are limited in size, the latter are not, but much slower), and the programmer did not even notice (this is only reasonable if you have variable integers, so this is not the way it is done in Smalltalk, but it could be :)

Another case may be loading the instance from the serial form back into the running system and updating its class to the latest version.

+2
source

Yes, look at #become in Smalltalk (e.g. MIT, licensed by Pharo.org).

In addition to the above examples, #, for example, is useful when you are working with a proxy. Think of a proxy object within ORM, such as Glorp, where you first have a proxy server, and when you need a real full object, it can be downloaded from the database and easily switch to all links.

Another example is the fuel rail in Faro.

+2
source

I don't know if this is suitable, but perhaps using a spy (partial layouts) is also suitable for your description (see http://docs.mockito.googlecode.com/hg/1.9.5/org/mockito/Spy.html )

Example:

 Person person = new Person(); person = spy(person); doReturn("dominiek").when(person).getName(); 

Behind the scenes, a subclass is created, and the class’s behavior changes according to the user behavior declarations.

+1
source

I just ran an instance of this in the NLTK source (Python). LazyCorpusLoader (the object used to load a dataset from disk) "turns" into the dataset itself. Here is the corresponding section of the associated source code (creating a dataset object and then creating it):

  corpus = self.__reader_cls(root, *self.__args, **self.__kwargs) # This is where the magic happens! Transform ourselves into # the corpus by modifying our own __dict__ and __class__ to # match that of the corpus. args, kwargs = self.__args, self.__kwargs name, reader_cls = self.__name, self.__reader_cls self.__dict__ = corpus.__dict__ self.__class__ = corpus.__class__ 

Here is the rationale (in the header of the same file) for this technique:

LazyCorpusLoader is a proxy object that is used for the chassis object prior to loading the chassis. This allows NLTK to create an object for each enclosure, but defer the associated costs of loading these enclosures until the first actually access.

Thus, the goal of changing the class at runtime in this case is to emulate a lazy evaluation .

(Edit: since I am quoting the shorthand from the NLTK source (Apache 2.0 license), here is a mandatory link to the license itself: http://www.apache.org/licenses/LICENSE-2.0 )

+1
source

Source: https://habr.com/ru/post/978319/


All Articles