c++ - 转类 "Interfaceable"

标签 c++ interface

在我公司的系统中,我们使用类来表示 bean。它只是使用 boost::variant 和一些序列化/反序列化内容的信息持有者。它运行良好,但我们有一个问题:它不是基于接口(interface),并且由于我们通过 dll 使用模块化,因此为它构建接口(interface)变得非常复杂,因为它几乎在我们应用程序的每个部分中使用,并且遗憾的是接口(interface)c++ 上的(抽象类)必须通过指针访问,这使得重构整个系统几乎不可能。

我们的结构是:

dll A:通过抽象类定义接口(interface)
dll B:接口(interface)实现

有一种轻松的方法可以实现这一目标(也许使用模板,我不知道)或者我应该忘记让这项工作有效并简单地将所有内容与 dll B 链接起来?

谢谢

编辑:这是我的例子。
这是在 dll A
BeanProtocol 是 N 数据协议(protocol)的持有者,通过索引访问。

class DataProtocol;

class UTILS_EXPORT BeanProtocol
{
public:
  virtual DataProtocol& get(const unsigned int  ) const
  {
    throw std::runtime_error("Not implemented");
  }

  virtual void getFields(std::list<unsigned int>&) const
  {
    throw std::runtime_error("Not implemented");
  }

  virtual DataProtocol& operator[](const unsigned int )
  {
    throw std::runtime_error("Not implemented");
  }

  virtual DataProtocol& operator[](const unsigned int ) const
  {
    throw std::runtime_error("Not implemented");
  }

  virtual void fromString(const std::string&) 
  {
    throw std::runtime_error("Not implemented");
  }

  virtual std::string toString() const
  {
    throw std::runtime_error("Not implemented");
  }

  virtual void fromBinary(const std::string&)
  {
    throw std::runtime_error("Not implemented");
  }

  virtual std::string toBinary() const
  {
    throw std::runtime_error("Not implemented");
  }

  virtual BeanProtocol& operator=(const BeanProtocol&)
  {
    throw std::runtime_error("Not implemented");
  }

  virtual bool operator==(const BeanProtocol&) const
  {
    throw std::runtime_error("Not implemented");
  }

  virtual bool operator!=(const BeanProtocol&) const
  {
    throw std::runtime_error("Not implemented");
  }

  virtual bool operator==(const char*) const
  {
    throw std::runtime_error("Not implemented");
  }

  virtual bool hasKey(unsigned int field) const
  {
    throw std::runtime_error("Not implemented");
  }
};

另一个类(名为 GenericBean)实现它。这是我发现使这项工作起作用的唯一方法,但现在我想将其转换为真正的界面并删除 UTILS_EXPORT(这是一个 _declspec 宏),最后删除 B 与 A 的强制链接。

最佳答案

我不太确定你的意思,但如果问题是 BeanProtocol 不是一个指针,那么你不能让它包装一个指向另一个类(比如 BeanPointerImpl)的指针,然后你可以从你的 DLL 加载它.

class BeanProtocol
{
  private BeanProtocolImpl m_impl;
  public:
  DataProtocol& get(const unsigned int index ) const
  {
    if(! m_impl)
    { 
       load_impl(get_appropriate_dll()) 
    }
    return m_impl->get(index);
  }

... BeanProtocol 上的所有其他方法等等。

然后您可以将您的插件实现为 BeanProtocolImpl 的不同子类,从您的 DLL(在本例中为 DLL B)导出它们,并废弃 DLL A。

在此示例中,我假设 load_impl 加载 DLL,调用工厂方法以获取 BeanProtocolImpl 的实现并将其存储在 m_impl 中,如果加载失败则抛出异常。

当然,您需要重新编译整个应用程序,但不需要重构它。

关于c++ - 转类 "Interfaceable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1970148/

相关文章:

python - 在 Cython 中定义 C++ 结构返回类型

c++ - 如何在 C/C++ 中打印上标 2?

c# - 对基类强制执行重写方法

c#-4.0 - 无法将 X 类型的对象转换为 Y

ruby - 在 Go 中建模 "superclass method implementation"的最佳方法是什么?

java - 继承和集合相关的编译错误

c++ - 如何使用 boost asio 通过 UDP 套接字发送 std::vector of unsigned char?

c++ - 在非基于 C/C++ 的图形界面中需要指导

haskell - 如何在 Haskell 中模拟继承?

c++ - 如何高效统计定义指针的个数?