工厂模式,实现工厂的动态列表
Factory pattern, implementing a dynamic list of factories
我正在实现一个抽象工厂模式(在 c++ 中),但有一个小问题。
我想避免创建一个在编译时必须知道存在哪些工厂的地方。
通常在示例中,我会看到类似的东西。
Factory * getFactory()
{
if(/*we should make factoryA*/)
{
return FactoryA::instance();
}
else if(/*we should return FactoryB*/)
{
return FactoryB::instance();
}
else
{
return NULL;
}
}
我可以做这样的事情,但我想要更好!
我想到的是工厂基类将有一个工厂列表,从工厂继承的每个类都会创建一个静态实例,并通过工厂中的受保护类函数将该实例添加到列表中。
但是,如果不玩带有静态对象初始化的俄罗斯轮盘赌,我就无法找到一种方法来做到这一点。
为了避免静态初始化顺序出现问题,您可以将列表设置为函数 getFactoryList() 的静态成员。 然后,这将确保当受保护的构造函数需要将工厂添加到列表中时,该列表存在。
然后,您需要将虚拟方法添加到工厂,以确定是否应使用给定的工厂。 希望一次只有一个工厂有资格使用,这样工厂创建的顺序就不会改变返回的工厂。
Somthing Simple:
class BaseFactory
{
public:
BaseFactory()
{
std::list<BaseFactory*>& fList = getFactoryList();
fList.push_back(this);
// If you are feeling brave.
// Write the destructor to remove the object from the list.
//
// Note C++ guarantees the list will live longer than any of the factories
// Because the list will always be completely constructed before any
// of the factory objects (because we get the list in the constructor).
}
static BaseFactory& getFactory() // Don't return a pointer (use a reference)
{
std::list<BaseFactory*>& fList = getFactoryList();
std::list<BaseFactory*>::iterator i = selectFactory(fList);
if (i == fList.end()
{
static FakeFactory fakeFactory; // Having a fake factory that
// that returns fake object
// is usually a lot easier than checking for
// NULL factory objects everywhere in the code
//
// Alternatively throw an exception.
return fakeFactory;
}
return *(*i); // return reference
}
private:
static std::list<BaseFactory*>& getFactoryList()
{
static std::list<BaseFactory*> factoryList; // Notice the static
return factoryList;
}
};
你会从这样的设计中受益吗?我的意思是,即使你有这个列表,你也必须根据一些标准以某种方式选择那个工厂。
请改为尝试反转控制模式。
如果类 A 需要创建一个对象,请将工厂传递给该类。
class A {
Factory *f;
public:
A(Factory *f)
: f(f)
{ }
void doSomething()
{
Object o = f->produce();
...
}
}
然后,您将决定在"更高级别"上使用哪个工厂。工厂可能来自源代码,表单插件等。
我使用一种模式来收集子类的实例。 这是它用工厂来表达的骨架:
class Factory {
public:
virtual Foo* makeFoo()=0;
...
protected:
Factory(){
getFactoryList().push_back(this);
}
private:
FactoryList& getFactoryList(); // Returns static list
};
class FactoryA: public Factory{
Foo* makeFoo(); // I make a Foo my way
} FACTORYINSTANCE;
您仍然需要一种方法来搜索列表以查找要使用的正确工厂,而我的宏 FACTORYINSTANCE 只是计算为唯一名称以调用其自己的构造函数。
相关文章:
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- C++中的动态铸造故障
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 内联映射初始化的动态atexit析构函数崩溃
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 控制允许动态运行c++的并发操作数
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 输出没有重复元素的动态数组(收缩数组)C++
- C++为线程工作动态地分割例程
- 正在插入动态数组
- 使用Unique_ptr确保工厂中的对象唯一
- 在c++中使用动态分配的问题
- C++中的动态对象与非动态对象
- 如何在动态数组上使用搜索函数
- 从工厂返回静态或动态分配的对象
- 有没有一种方法可以在c++中实现动态工厂模式
- 工厂模式,实现工厂的动态列表