Pergunta

I have a set of data objects like this...

class Image
{
    string FilePath { get; set; }
    byte[] Content { get; set; }
}

class ThingWithImages
{
    IList<Image> Images1 { get; set; }
    Image DifferentImage { get; set; }
}

class AnotherThingWithImages
{
    IList<Image> Images2 { get; set; }
}

I need to traverse all these objects with either and Image property or collection of Image objects and 1). load the file into the Content byte array, 2). modify the FilePath property. Later I then need to restore the Image object to its original state.

What is the best way to achieve this?

I originally defined an interface and implemented it on every object containing Image's

interface IContainImage
{
    void Load();
    void Restore();
}

But this is adding a lot of logic and additional state (for the Restore) to what were plain POCO data objects, so I don't like this solution.

Is Visitor pattern applicable here? Where to store the state data needed for the Restore operation?

Foi útil?

Solução

One idea that gets you halfway there. Have each image bearing class implement a method to return an enumerator for its images - GetImageEnumerator. Now your loader can call that to get everybody's images and then load them. This is a cheap way of implementing visitor.

To implement restore, have the loader keep an array with two things at each element - a reference to the image of jest you found and loaded, and a copy of the original byte array. Restore then just runs through this list and puts the arrays back.

This design assumes that after the initial load, new image objects won't be created.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top