【发布时间】:2020-11-05 08:01:23
【问题描述】:
我有以下课程:
class Document
{
public:
Document():
// default values for members,
// ...
m_dirty{false}{}
// Accessor functions
template<class OutputStream>
Document& save(OutputStream stream)
{
// Write stuff to `stream`
// ...
m_dirty = false;
return *this;
}
bool dirty() const { return m_dirty; }
private:
Size2d m_canvas_size;
LayerStack m_layers;
LayerIndex m_current_layer;
std::vector<Palette> m_palettes;
PaletteIndex m_current_palette;
ColorIndex m_current_color;
std::vector<std::string> m_palette_names;
std::vector<std::string> m_layer_names;
bool m_dirty;
};
该类是否应该具有用于直接修改say m_palettes 元素的公共成员函数,例如
Document& color(PaletteIndex, ColorIndex, Color)
,或者更“正确”,只允许通过一对API访问整个向量:s
std::vector<Palette> const& palettes();
Document& palettes(std::vector<Palette>&&);
第一个选项会更有效,因为它不需要创建数据成员的临时副本,但始终使用这种设计会使接口变得臃肿。对于类中的每个容器,它都需要“深度”的 getter 和 setter。
注意脏标志。因此,以下内容会破坏抽象:
std::vector<Palette>& palettes();
【问题讨论】:
-
你可能有代理来“传播”来自
Palette修改的脏标志。 -
@Jarod42 所以基本上是一个具有对容器和脏标志的私有引用的对象,然后是该代理上的适当方法。听起来是个不错的方法。
-
... 并且代理的析构函数设置了脏标志。但它必须有一个名字。
标签: c++ api-design object-composition