开发者

Factory method pattern

What exactly is the factory method? How can it be impleme开发者_如何学运维nted with dll?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜