工厂模式在C + + - 这样做正确吗? [英] Factory Pattern in C++ -- doing this correctly?

查看:124
本文介绍了工厂模式在C + + - 这样做正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对于设计模式相对较新,因为它们在形式上被称为。我不是一个专业的很长时间,所以我是新的这个。

I am relatively new to "design patterns" as they are referred to in a formal sense. I've not been a professional for very long, so I'm pretty new to this.

我们有一个纯虚拟接口基类。这个接口类显然提供了它的派生孩子应该做什么功能的定义。软件中的当前使用和情况决定了我们想要使用的派生子类型,因此我建议创建一个包装器,它将传达我们想要的派生子类型,并返回一个指向新派生对象的Base指针。根据我的理解,这个包装是一个工厂。

We've got a pure virtual interface base class. This interface class is obviously to provide the definition of what functionality its derived children are supposed to do. The current use and situation in the software dictates what type of derived child we want to use, so I recommended creating a wrapper that will communicate which type of derived child we want and return a Base pointer that points to a new derived object. This wrapper, to my understanding, is a factory.

好吧,我的一个同事在Base类中创建了一个静态函数作为工厂。这导致我麻烦有两个原因。首先,它似乎打破了Base类的接口性质。我觉得这个界面本身需要知道从它派生的孩子的知识。

Well, a colleague of mine created a static function in the Base class to act as the factory. This causes me trouble for two reasons. First, it seems to break the interface nature of the Base class. It feels wrong to me that the interface would itself need to have knowledge of the children derived from it.

其次,当我尝试重新使用基类跨两个不同的Qt项目。一个项目是我实现的第一个(也许只有真正的实现这一个类...虽然我想使用相同的方法为两个其他功能,将有几个不同的派生类)派生类,第二是实际应用程序,我的代码最终将被使用。我的同事创建了一个派生类作为实际应用程序的测试人员,而我编码我的部分。这意味着我必须添加他的头和cpp文件到我的项目,这似乎是错误的,因为我甚至不使用他的代码为项目,而我实现我的部分(但他会使用我的,当它完成)。

Secondly, it causes more problems when I try to re-use the Base class across two different Qt projects. One project is where I am implementing the first (and probably only real implementation for this one class... though i want to use the same method for two other features that will have several different derived classes) derived class and the second is the actual application where my code will eventually be used. My colleague has created a derived class to act as a tester for the real application while I code my part. This means that I've got to add his headers and cpp files to my project, and that just seems wrong since I'm not even using his code for the project while I implement my part (but he will use mine when it is finished).

我是否认为工厂真的需要是一个Base类的包装器而不是作为工厂的Base?

Am I correct in thinking that the factory really needs to be a wrapper around the Base class rather than the Base acting as the factory?

推荐答案

你不想使用你的接口类作为工厂类。一个,如果它是一个真正的接口类,没有实现。第二,如果接口类有一些实现定义(除了纯虚函数),现在强制基类每次添加一个子类实现时重新编译基类。

You do NOT want to use your interface class as the factory class. For one, if it is a true interface class, there is no implementation. Second, if the interface class does have some implementation defined (in addition to the pure virtual functions), making a static factory method now forces the base class to be recompiled every time you add a child class implementation.

实现工厂模式的最好方法是让你的接口类与你的工厂分离。

The best way to implement the factory pattern is to have your interface class separate from your factory.

一个非常简单如下:

class MyInterface
{
public:
    virtual void MyFunc() = 0;
};

class MyImplementation : public MyInterface
{
public:
    virtual void MyFunc() {}
};

class MyFactory
{
public:
    static MyInterface* CreateImplementation(...);
};

这篇关于工厂模式在C + + - 这样做正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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