类继承/组合的设计 [英] design of class inheritance/compostion
问题描述
我有两个类:一个读者和一个作家。对于这两个类,我有一个抽象的接口,因为数据源/目标应该是灵活的:
I do have two classes: a reader and a writer. For both classes I have an abstract interface since the data source/target should be flexible:
class abstract_reader {
...
};
class concrete_reader : public abstract_reader {
DATATYPE m_data;
...
};
class abstract_writer {
...
};
class concrete_writer : public abstract_writer {
DATATYPE m_data;
...
};
写入器应具有读取和写入功能。具体写入器的读取部分的实现是与实现的concrete_reader相同。什么是合并这些类的好方法?
The writer shall have both functionalities, to read and to write The implementation of the reading part of the concrete_writer is the same as the implementation of the concrete_reader. What would be a good way to combine these classes?
推荐答案
你想要的是有点混乱,方法:
What you want is a little confusing, but there is a number of ways:
- 从两个抽象类派生作者
- 从写入器中删除读取功能,并创建第三个类,它可以读取和写。
就我个人而言,我会遵循最后一种方式,那么你将有一套完整的类来进行只读,和做这两个。
Personally, I would follow the last way, then you will have a full set of classes for reading-only, writing-only, and for doing both.
这篇关于类继承/组合的设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!