Factory method pattern

1k views Asked by At

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

2

There are 2 answers

0
Tony Delroy On BEST ANSWER

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

0
ayush On

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