如何在C ++中正确设计和实现CopyConstructable接口?

时间:2016-03-14 04:08:43

标签: c++ oop design-patterns interface copy-constructor

This question是当前的前身。需要提出更好的OOP设计。

问题描述:有一个名为example.com/subsite/subsite/different_index.html 的类,它使用类FileWriter的对象将键值(Formatter)的字典写入a文件。

要求:

  1. 使用现有格式化程序初始化std::map<std::string, double>
  2.     JsonFormatter jf;
        FileWriter fw("test.txt", jf);
    
    1. 使用r值初始化FileWriter
    2.     FileFormatter fw("test.txt", CsvFormatter());
      
      1. 应该有FileWriter类的层次结构(派生自接口Formatter)以允许以下内容:
      2.     {
                CsvFormatter cf(2, "\t"); // 2 digits precision, tab-separated
                FileWriter fwCsv("test.txt", csv);
            }
            {
                JsonFormatter jf(2, true); // 2 digits precision, compact JSON formatting
                FileWriter fwJson("test.txt", jf);
            }
        
        1. IFormatter可以存储其他参数,例如精度,分隔符号(对于CSV)或紧凑格式标记(对于JSON)。
        2. 这种问题的正确设计模式和正确实施是什么?我主要担心的是:

          • Formatter应存储指向FileFormatter(基类)的指针,因为它将是抽象类
          • 指向IFormatter的指针需要在IFormatter构造函数中传递指针,这会增加实现的复杂性以满足要求1

          目前在C ++中如何完成?这种情况是否属于source/sink成语?现有解决方案的任何链接也是受欢迎的。

0 个答案:

没有答案