Design pattern for multiple output formats

Viewed 4934

I have a class structure which represents (internally) the data I wish to output to a file.

Some of the member variables are private to the data class so that it can manage itself and stop things going awry.

I then want this data to be output into a number of file formats. I could do something like

savefile_formatA(DataClass* pDataClass, ofstream& fout);
savefile_formatB(DataClass* pDataClass, ofstream& fout);

except that the functions need to then see the private member variables of DataClass. I could of course just make savefile_formatXYZ() friend functions but then I would need to add a friend declaration for every different format.

Is there a standard design pattern for solving this kind of thing? How would you solve this problem?

Thanks!

7 Answers
Related