Question

Due to the existing structure of code, which looks like this:

// Read buffer (using ByteBuffer)
// Pass buffer to a remote method, which MIGHT write to it
// Write buffer back if dirty

I want that my buffer should be able to identify if it is written to (if it's dirty). I didn't find any buffer implementation in java with a dirty flag.

In all these cases my code is already passing around ByteBuffers, in order to add this dirty flag should I:

  • simply wrap the ByteBuffer implementation with dirty flag
  • Write a class with underlying byte array, add dirty big and other operations (get/put int or long) and re-create the ByteBuffer by wrapping around the byte array
  • Extend the ByteBuffer class? (I'd be happy to implement the get/put but there are WAY TOO many methods to be implemented)

Also, what are the overheads of wrapping the ByteBuffer implementation?

Was it helpful?

Solution

This is a problem where the decorator pattern applies. You are strictly adding simple functionality to an existing class that's already in use. In this case, extending the class by inheritance is a natural and low threat implementation.

The overhead in Java of using the extended class vs. the base class is essentially zero. I am not counting as "overhead" the space for the dirty flag and the few instructions needed to implement it. But these costs are very low as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top