类继承/组合的设计 [英] design of class inheritance/compostion

查看:146
本文介绍了类继承/组合的设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类:一个读者和一个作家。对于这两个类我有一个抽象接口,因为数据源/目标应该是灵活的:

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_writer的阅读部分的实施是与实现的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:


  • 从两个抽象类

  • 导出作家保持一个指向具体的读者作家

  • 保持混凝土读者的一个实例作家

  • 从作家取下阅读功能,使第三类,可以读取和写。

  • Derive the writer from both abstract classes
  • Hold a pointer to the concrete reader in the writer
  • Hold an instance of the concrete reader in the writer
  • Remove the reading functionality from the writer and make a third class that can both read and write.

就个人而言,我会按照过去的方式,那么你将有一个完整的组类用于读取只,只写,和做这两个。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆