工厂方法模式

发布于 2024-10-16 21:25:56 字数 28 浏览 1 评论 0原文

工厂方法到底是什么?怎么用dll来实现呢?

What exactly is the factory method? How can it be implemented with dll?

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

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

发布评论

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

评论(2

浅暮の光 2024-10-23 21:25:56

工厂的基本思想是函数返回从已知基类派生的堆分配对象。因此,在您的情况下,您的主应用程序中可能有一些代码调用 dll 中的工厂,返回未知动态类型和实现的对象,但您会知道它满足特定基类的 API 要求。然后,您可以通过该接口使用该对象,并通过其可能的虚拟析构函数将其删除。工厂方法有多种类型,具体取决于实际派生类的选择方式,但通常它会检查函数的一些输入、IO 流或 XML 结构等,并计算出适当的类型。工厂是否位于 dll 中并不会对整体模型产生任何影响,但它确实使更新派生对象的列表和实现变得更加容易,而无需重新编译应用程序。

有关更多详细信息,请参阅:http://en.wikipedia.org/wiki/Factory_method_pattern

The basic idea with a factory is that a function returns a heap-allocated object derived from a known base class. Thus, in your situation you'd presumably have some code in your main application that calls a factory in the dll, getting back an object of unknown dynamic type and implementation, but you'll know it satisfies the API requirements of a specific base class. You then use the object via that interface, and delete it via its presumably virtual destructor. There are a number of types of factory method depending on how the choice of actual derived class is made, but typically it examines some inputs to the function, an IO stream or XML structure etc., and works out an appropriate type. Whether the factory is in a dll or not doesn't really make any difference to the overall model here, but it does make it easier to update the list and implementation of derived objects without recompiling the application.

For more details, see: http://en.wikipedia.org/wiki/Factory_method_pattern

夏九 2024-10-23 21:25:56

工厂方法是一种创建模式。此模式有助于对用于创建对象的接口进行建模,该对象在创建时可以让其子类决定要实例化哪个类。我们称其为工厂模式,因为它负责“制造”对象。它通过从一组相关类创建正确的对象来帮助实例化适当的子类。工厂模式消除了将特定于应用程序的类绑定到代码中的需要,从而促进了松散耦合。

工厂模式的全部内容是“定义一个用于创建对象的接口,但让子类决定实例化哪个类。工厂方法让类将实例化推迟到子类”因此:“工厂方法让类将实例化推迟到子类”。

请参阅此一个组件对象模型 (COM) 应用程序中工厂模式的典型使用

Factory Method is a creational pattern. This pattern helps to model an interface for creating an object which at creation time can let its subclasses decide which class to instantiate. We call this a Factory Pattern since it is responsible for "Manufacturing" an Object. It helps instantiate the appropriate Subclass by creating the right Object from a group of related classes. The Factory Pattern promotes loose coupling by eliminating the need to bind application-specific classes into the code.

The Factory Pattern is all about "Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses" Thus: "The Factory Method lets a class defer instantiation to subclasses".

See this for One typical use of the Factory Pattern in a Component Object Model (COM) application

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