考虑这个例子
Gabi::Herbs::Filesystem::FileReader filereader
{
Gabi::Herbs::Filesystem::FileIn
{Gabi::Herbs::Filesystem::Path(GABI_HERBS_STR("herbs/textio/test_utf8.txt")),0}
,0
};
Gabi::Herbs::IO::ReaderBuffering reader(filereader,128);
Gabi::Herbs::TextIO::Decoder decoder(reader,Gabi::Herbs::TextIO::ConverterUTF8::factory);
它是C ++,但它可以是任何支持OOP的语言。所以
在这种情况下如何创建包装器,因此我不需要创建三个对象和两个临时对象?可以使用合并类,但是会失去一些灵活性。
答案 0 :(得分:1)
也许创建一个新类,如下所示:
class FileReaderDecoder
{
Gabi::Herbs::Filesystem::FileReader filereader;
Gabi::Herbs::IO::ReaderBuffering reader;
Gabi::Herbs::TextIO::Decoder decoder;
public:
FileReaderDecoder(std::string file_name)
{/*Initialize the three member variables like you did in your own code*/}
//Add accessor functions here to get the data from the decoded file, e.g.:
std::string GetData(int start, int size);
}
然后呼叫减少到例如
FileReaderDecoder file_rd_dec("herbs/textio/test_utf8.txt");
file_rd_dec.GetData(0, 16);