[英]How to get rid of long initialization without losing flexibililty
考慮這個例子
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的語言。 所以
在這種情況下,如何創建包裝器,因此不需要創建三個對象和兩個臨時對象? 可以使用合並類,但是會失去一些靈活性。
也許像下面這樣創建一個新類:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.