寻找一种设计模式以应用于C++

Looking for a design pattern to apply in C++

本文关键字:设计模式 应用于 C++ 一种 寻找      更新时间:2023-10-16

我有这些C++类定义如下:

class A
{
public:
    B *createB();
};
class B 
{
public:
    virtual void fun() = 0;
};
class B1 : B {/* ... */};
class B2 : B {/* ... */};

所以基本上 B 是一个抽象类,B1 和 B2 是 B 的具体实现,A 在其代码中的某个地方创建了一个类型 B 的实例。需要注意的是,A 不是工厂A::createB只是一个例子。

我希望能够在 A 初始化期间传递 B 的子类,以便可以在运行时根据需要由后者创建前者的实例。例:

A *a1 = /* sorcery here */;
A *a2 = /* another magic code */;
a1->createB(); // getting B1
a2->createB(); // getting B2

实现它的最佳方法是什么?是否可以不使用模板?

<小时 />

根据回应,我最终得到了这个。谢谢!

class B
{
public:
    virtual void fun() = 0;
    virtual B *clone() = 0;
};
class B1 : public B
{
public:
    virtual void fun()
    {
        std::cout << "B1" << std::endl;
    }
    virtual B *clone()
    {
        return new B1();
    }
};
class B2 : public B {/* analogous to B1 */};
class A
{
public:
    A(B *b) : b(b) {};
    B *createB()
    {
        return b->clone();
    }
private:
    B *b;
};
A(new B1()).createB()->fun(); // prints "B1"
A(new B2()).createB()->fun(); // prints "B2"

在 B 中实现一个clone()方法。

创建 A 时将B*传递给 A。A 将使用该B*作为参数调用 B 的clone()

有关克隆的更多信息,请参阅问题 哪种情况将在C++中使用克隆以及如何使用它?、C++中克隆() 的最佳签名是什么?以及如何轻松编写克隆方法?等。

您可以使用原型设计模式来实现此目的。A传递 B1B2 的实例,并向B添加一个 clone() 成员函数,如下所示:

class B 
{
public:
    virtual void fun() = 0;
    virtual B* clone() = 0; // B1::clone returns new B1; B2::clone returns new B2
};

A存储初始化期间传入的B的原型实例供以后使用。当它稍后需要创建新B时,它会调用原型上的clone()以获取正确类的实例。

听起来你想要抽象工厂。

你可以在这里看到例子:

#include <iostream>
#include <string>
class Window
{
protected:
int width;
int height;
std::string toolkit;
std::string type;
Window(std::string usedToolkit, std::string windowType)
: toolkit(usedToolkit), type(windowType)
{}
public:
std::string getToolkit()
{
return toolkit;
}
std::string getType()
{
return type;
}
};
class GtkToolboxWindow : public Window
{
public:
GtkToolboxWindow()
: Window("Gtk", "ToolboxWindow")
{}
};
class GtkLayersWindow : public Window
{
public:
GtkLayersWindow()
: Window("Gtk", "LayersWindow")
{}
};
class GtkMainWindow : public Window
{
public:
GtkMainWindow()
: Window("Gtk", "MainWindow")
{}
};

class QtToolboxWindow : public Window
{
public:
QtToolboxWindow()
: Window("Qt", "ToolboxWindow")
{}
};
class QtLayersWindow : public Window
{
public:
QtLayersWindow()
: Window("Qt", "LayersWindow")
{}
};
class QtMainWindow : public Window
{
public:
QtMainWindow()
: Window("Qt", "MainWindow")
{}
};

/* This is the abstract factory. */
class UIFactory
{
public:
virtual Window* getToolboxWindow() = 0;
virtual Window* getLayersWindow() = 0;
virtual Window* getMainWindow() = 0;
};
/* Factory for Gtk toolkit */
class GtkUIFactory : public UIFactory
{
public:
Window* getToolboxWindow()
{
return new GtkToolboxWindow();
}
Window* getLayersWindow()
{
return new GtkLayersWindow();
}
Window* getMainWindow()
{
return new GtkMainWindow();
}
};
/* Factory for Qt toolkit */
class QtUIFactory : public UIFactory
{
public:
Window* getToolboxWindow()
{
return new QtToolboxWindow();
}
Window* getLayersWindow()
{
return new QtLayersWindow();
}
Window* getMainWindow()
{
return new QtMainWindow();
}
};
int main()
{
UIFactory* ui = 0;
/* Check what environment is running
and create appropriate factory. */
if (/* Gtk == */ true)
{
ui = new GtkUIFactory();
}
else
{
ui = new QtUIFactory();
}
/* Use the factory to build interface. */
Window* toolbox = ui->getToolboxWindow();
Window* layers = ui->getLayersWindow();
Window* main = ui->getMainWindow();
/* See what have we recieved. */
std::cout << toolbox->getToolkit() << ":"
<< toolbox->getType() << std::endl;
std::cout << layers->getToolkit() << ":"
<< layers->getType() << std::endl;
std::cout << main->getToolkit() << ":"
<< main->getType() << std::endl;
}