I could use the access methods in the Pixel structure instead of simple fields and give a link to my owner to call home. This, however, would mean that if the user changed R, G, B, and A, I would convert the pixel 4 times to a raster pixel format and write to memory 4 times.
This pushes the demeter law (the pixel does not have to modify the bitmap to set its own value); You must not do this.
I could return the pixel reference from the iterator and provide it with the Update () method, which needs to be called if the pixel has been changed. This would be unintuitive, and users at the risk of forgetting to call Update.
This is a bit of a bad interface, as it will be difficult to use correctly without forgetting anything. This also breaks the demeter strip (you should not update the bitmap through the pixel).
I could always return a Pixel value by value and provide a special assignment operator. Breaks the standard iterator pattern - assigning iterator without dereferencing should move the iterator, and not update the element pointing to
Do not do this; this violates the principle of least surprise.
Consider instead to make the pixel independent of the bitmap (i.e., the pixel does not know what the bitmap is).
You should be able to set (real) values ββto a pixel easily (using accessories for fields or Update(R, G, B, A) or similar) and have a bitmap, know what a pixel is and how to update yourself from pixels, with one (or more):
void Bitmap::Update(int x, int y, const Pixel& p); void Bitmap::Fill(const Pixel& p); void Bitmap::SetRange(SomeRangeObject r, const Pixel& p);
Thus, the update operation for pixels will require the following steps:
- get pixels from a bitmap
- update / convert pixels as needed
- refresh pixel bitmap
This does not apply to those who will know the positions of the pixels (they probably should not be part of the pixel itself), but this is the beginning.
In this implementation, your interdependencies remain low (the functionality for updating the pixels of the bitmap remains in the bitmap, not the pixels), and the iterative model is trivial (the same as standard iterators).