简体   繁体   English

一类的多个接口

[英]Multiple interfaces to one class

I am trying to come up with an elegant solution for the problem of offering multiple interfaces to a class in C++. 我正在尝试为在C ++中为类提供多个接口的问题提出一种优雅的解决方案。 Suppose we have the classes B,C and D. B and C need customized/controlled access to the class A whereas D can have direct access. 假设我们具有类B,C和D。B和C需要对类A的自定义/受控访问,而D可以具有直接访问。 The solution for offering such interfaces needs to be extendable. 提供此类接口的解决方案需要可扩展。 Future interfaces might be specialized for only one particular class. 将来的接口可能仅专用于一个特定的类。

Currently I am creating interface classes (IA_1, IA_2) which have a reference to the class A. B and C will be provided whith instances of those interfaces and can access A through IA_1 respectively IA_2 in a customized/controlled manner. 当前,我正在创建引用类A的接口类(IA_1,IA_2)。将为这些接口的实例提供B和C,它们可以以定制/受控方式分别通过IA_1和IA_2访问A。 The situation is depicted in the image below. 下图描述了这种情况。

在此处输入图片说明

This has the advantage that I don't need to touch class A when implementing a new interface. 这样做的好处是,在实现新接口时,我不需要接触A类。 New interfaces can utilize old interfaces through inheritance. 新接口可以通过继承利用旧接口。 Classes which need access to A will only be able to do through their particular interface. 需要访问A的类只能通过其特定的接口进行操作。

The implementation in C++ would look like this: C ++中的实现如下所示:

class A{
public:
  void foo();
};

class IA_1{
public:
  void foo(const B& b);
private:
  std::weak_ptr<A> m_A;
};

class IA_2{
public:
  void foo();
private:
  std::weak_ptr<A> m_A;
};

class B{
  std::unique_ptr<IA_1> m_A;
};

class C{
  std::unique_ptr<IA_1> m_A;
};

class D{
  std::weak_ptr<A> m_A;
};

The interfaces get a weak_ptr to A because I don't want classes which are only accessing A to participate in its lifetime management. 接口获得A的weak_ptr,因为我不希望仅访问A的类参与其生命周期管理。

Currently I am creating an interface object for every instance of B or C, although they are all doing the same. 目前,我正在为B或C的每个实例创建一个接口对象,尽管它们都在做相同的事情。 I already thought about creating only one interface object and give every instance of B and C a reference (shared_ptr) of the corresponding interface. 我已经考虑过只创建一个接口对象,并为B和C的每个实例提供相应接口的引用(shared_ptr)。 In that way I will only have one object for every interface. 这样,每个接口我只有一个对象。 Maybe this is already a case of premature optimization to reduce the memory footprint of the application. 也许这已经是过早优化以减少应用程序内存占用的情况。

Is there a way to further improve this design or take a totally different approach? 有没有办法进一步改进此设计或采取完全不同的方法?

Probably not a complete answer, just my thoughts. 可能不是一个完整的答案,仅是我的想法。

Cons: 缺点:

  • Extra interface classes introduced 引入了额外的接口类
  • Extra indirection, with extra maintenance per supported call 额外的间接访问,每个受支持的呼叫需要额外的维护
  • construction: extra factory needed for interface pointing to object 构造:指向对象的接口需要额外的工厂
  • weak pointers: solve the case where the pointer becomes dangling 弱指针:解决指针悬空的情况

Pros: 优点:

  • clear control over interface to the class 明确控制类的接口
  • extra safety over lifetime (can be checked, but should also be handled) 终生额外的安全性(可以检查,但也应加以处理)
  • limited change needed on object (friends?) 需要在对象上进行有限的更改(朋友?)

About the 1 instance per interface: The footprint will be a bit lower, but you need extra management over accessing that 1 instance. 关于每个接口1个实例:占用空间会减少一些,但是您需要对访问该1个实例进行额外的管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM