将接口“ISingleton”抽象为基类

发布于 2024-08-05 21:15:08 字数 739 浏览 6 评论 0原文

我有 3 个接口类 IVideo 、 IAudio 、 IGPIO 和其他三个将实现这些接口的类: Video_impl 、 Audio_impl 、 GPIO_impl

到目前为止事情很简单。

但是,我希望所有这些对象都是单例。以下是问题:

抽象一个接口 ISingleton 是否是一个好主意,以便 Video_impl 、 Audio_impl 、 GPIO_impl (或 IVideo 、 IAudio 、 IGPIO > ?) 可以继承它吗?

我正在考虑通过以下方式实现它。推荐吗?我认为一定有更好的方法。

//Isingleton.hpp
template <class T>
class ISingleton
{
public:
virtual T *getInstance() = 0;

};

class IGPIO
{
public:
virtual int SelectAudioInput() = 0;
};

class GPIO_impl : public IGPIO, public ISingleton<IGPIO>
{
public:
    IGPIO *getInstance();
    int SelectAudioInput() ;
private:
    IGPIO *instance;
};

I have 3 interface classes IVideo , IAudio , IGPIO and three other classes that will implement those interface: Video_impl , Audio_impl , GPIO_impl.

Things is simple so far.

But then ,I want all those object to be singleton. Here are the questions:

Is it a good idea to abstract an Interface ISingleton , so that Video_impl , Audio_impl , GPIO_impl (or IVideo , IAudio , IGPIO ?) can inherit from it?

I was thinking of implement it in following way. Is it recommended? I think there must be better ways.

//Isingleton.hpp
template <class T>
class ISingleton
{
public:
virtual T *getInstance() = 0;

};

class IGPIO
{
public:
virtual int SelectAudioInput() = 0;
};

class GPIO_impl : public IGPIO, public ISingleton<IGPIO>
{
public:
    IGPIO *getInstance();
    int SelectAudioInput() ;
private:
    IGPIO *instance;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鱼窥荷 2024-08-12 21:15:09

我没有看到这里有什么好处。您仍然必须在派生类中实现 getInstance() 方法。所以你引入了模板和多重继承,但并没有真正获得任何东西。我能看到的唯一的小收获是你让所有的单身人士共享一个通用的方法,但这是一个如此常见的模式,我不认为这是一个真正的收获。

I don't see the benefit here. You still have to implement the getInstance() method in your derived class. So you've introduced templates and multiple inheritance, but haven't really gained anything. The only small gain I can see is you're making all your singleton's share a common method, but this is such a common pattern that I don't think that's a real gain.

蹲在坟头点根烟 2024-08-12 21:15:09

你的单身人士将无法工作。您不能使用虚拟方法返回实例,因为只能使用实例调用它。您至少需要一个静态方法,或者一个实现单例的中间对象。

如果您打算像这样概括它,您应该阅读 C++ 中单例的实现。然而,我对所有库代码的建议是首先创建您需要的干净简单的实现。在单例情况下,这将简单地将其实现为没有继承的静态方法。然后,只有当您多次实现它并完全理解抽象实现的所有动机和细节时,您才可以为其创建一个库。过早创建库是导致错误和代码不可维护的最大原因之一。

Your singleton won't work. You cannot use a virtual method to return the instance as this can only be called with an instance. You need at the very least a static method, or an intermediate object that implements the singleton.

You should read up on the implementation of singletons in C++ if you plan to generalise it like this. However my advice for all library code is to create the clean simple implementations you need first. In the singleton case this would be simply implement it as a static method with no inheritance. Then and only when you have implemented it a number of times and fully understand all the motivations and details for an abstract implementation should you create a library for it. Creating libraries too early is one of the largest causes of bugs and unmaintainable code.

苯莒 2024-08-12 21:15:08

我建议阅读 Alexandrescu 的“Modern C++ Design”。其中,除其他外,他设计了一个成熟的单例模板,并思考了许多问题,例如何时应该销毁它,是否应该在销毁后复活,因为在销毁其他单例时需要它,以及所有这些好东西。

I would recommend reading Alexandrescu's "Modern C++ Design". In it, among many other things, he designs a fully-fledged singleton template and thinks through many of the issues, such as when it should be destroyed, whether it should resurrect after being destroyed because it is needed during the destruction of other singletons, and all that good stuff.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文