有没有办法在 C++ 中实现动态工厂模式?
Is there a way to implement dynamic factory pattern in c++?
The DYNAMIC FACTORY pattern describes how to create a factory that
allows the creation of unanticipated products derived from the same
abstraction by storing the information about their concrete type in
external metadata
来自 : http://www.wirfs-brock.com/PDFs/TheDynamicFactoryPattern.pdf
PDF 说:
Configurability
. We can change the behavior of an application by just changing its configuration
information. This can be done without the need to change any source code (just change the descriptive information about the type in the metadata repository) or to restart the application (if caching is not used – if caching is used the cache will need to be flushed).
在不修改源代码的情况下,无法将新类型引入 运行 C++ 程序。至少,您需要编写一个包含工厂的共享库来生成新类型的实例:但这样做被 PDF 明确排除:
Extensibility / Evolvability
. New product types should be easily
added without requiring neither a
new factory class nor modifying
any existing one.
这在 C++ 中不实用。
不过,功能可以通过使用元数据来指导一些代码编写功能,然后调用编译器(无论是作为子进程还是库)创建共享库来实现.这几乎就是 PDF 中提到的语言在使用反射和元数据要求虚拟机创建新的 class 实例时所做的事情:在那些语言环境中需要 compiler/interpreter 在内存中徘徊,所以它似乎并没有那么大的一步。
是的...
查看 Factories classes in the Qtilities Qt 库。
@TonyD 关于
We can change the behavior of an application by just changing its configuration information.
如果你换一种方式来理解这句话,那是100%的可能。我阅读和理解的是,您更改了一个配置文件(文档中的 xml),该文件被加载以更改应用程序的行为。所以也许您的应用程序有 2 个记录器,一个用于归档,一个用于 GUI。因此可以编辑配置文件以选择要使用的一个或两个。因此,应用程序没有变化,但行为发生了变化。要求是您可以在文件中配置的任何内容都可以在代码中使用,所以说使用网络的日志将不起作用,因为它没有实现。
New product types should be easily added without requiring neither a new factory class nor modifying any existing one.
是的,这听起来有点不可能。我将接受无需更改原始应用程序即可添加的功能。因此,应该能够使用插件或其他方法添加,并保持 application/factory/existing 类 不变。
提供的示例支持以上所有内容。尽管 Qtilities 是一个 Qt 库,但工厂并不是特定于 Qt 的。
The DYNAMIC FACTORY pattern describes how to create a factory that allows the creation of unanticipated products derived from the same abstraction by storing the information about their concrete type in external metadata
来自 : http://www.wirfs-brock.com/PDFs/TheDynamicFactoryPattern.pdf
PDF 说:
Configurability . We can change the behavior of an application by just changing its configuration information. This can be done without the need to change any source code (just change the descriptive information about the type in the metadata repository) or to restart the application (if caching is not used – if caching is used the cache will need to be flushed).
在不修改源代码的情况下,无法将新类型引入 运行 C++ 程序。至少,您需要编写一个包含工厂的共享库来生成新类型的实例:但这样做被 PDF 明确排除:
Extensibility / Evolvability . New product types should be easily added without requiring neither a new factory class nor modifying any existing one.
这在 C++ 中不实用。
不过,功能可以通过使用元数据来指导一些代码编写功能,然后调用编译器(无论是作为子进程还是库)创建共享库来实现.这几乎就是 PDF 中提到的语言在使用反射和元数据要求虚拟机创建新的 class 实例时所做的事情:在那些语言环境中需要 compiler/interpreter 在内存中徘徊,所以它似乎并没有那么大的一步。
是的...
查看 Factories classes in the Qtilities Qt 库。
@TonyD 关于
We can change the behavior of an application by just changing its configuration information.
如果你换一种方式来理解这句话,那是100%的可能。我阅读和理解的是,您更改了一个配置文件(文档中的 xml),该文件被加载以更改应用程序的行为。所以也许您的应用程序有 2 个记录器,一个用于归档,一个用于 GUI。因此可以编辑配置文件以选择要使用的一个或两个。因此,应用程序没有变化,但行为发生了变化。要求是您可以在文件中配置的任何内容都可以在代码中使用,所以说使用网络的日志将不起作用,因为它没有实现。
New product types should be easily added without requiring neither a new factory class nor modifying any existing one.
是的,这听起来有点不可能。我将接受无需更改原始应用程序即可添加的功能。因此,应该能够使用插件或其他方法添加,并保持 application/factory/existing 类 不变。
提供的示例支持以上所有内容。尽管 Qtilities 是一个 Qt 库,但工厂并不是特定于 Qt 的。