views:

826

answers:

8

When to use Factory method pattern?

Please provide me some specific idea when to use it in project? and how it is a better way over new keyword?

+12  A: 

I have two cases where I tend to use it:

  • The object needs to be initialized in some specific manner
  • When I want to construct a specific type based on an abstract type (an abstract class or an interface).

An example of the first case could be that you want to have a factory creating SqlCommand objects, where you automatically attach a valid SqlConnection before returning the command object.

An example of the second case is if you have an interface defined and determine at execution time which exact implementation of the interface to use (for instance by specifying it in a configuration file).

Fredrik Mörk
A: 

To answer the second part of you question from my opinion, I think the reason it's better than the 'new' keyword is that the factory method reduces the dependancy on constructors of particular classes. By using a factory method, you delegate the creation of the object in question to someone else, so the caller doesn't need teh knowledge of how to create the object.

Jimmeh
+5  A: 

You can refer to section 9.5 Factories from Framework Design Guidelines 2nd Edition. Here is quoted set of guidelines with respect to using factories over constructors:

DO prefer constructors to factories, because they are generally more usable, consistent, and convenient than specialized construction mechanisms.

CONSIDER using a factory if you need more control than can be provided by constructors over the creation of the instances.

DO use a factory in cases where a developer might not know which type to construct, such as when coding against a base type or interface.

CONSIDER using a factory if having a named method is the only way to make the operation self-explanatory.

DO use a factory for conversion-style operations.

And from section 5.3 Constructor Design

CONSIDER using a static factory method instead of a constructor if the semantics of the desired operation do not map directly to the construc- tion of a new instance, or if following the constructor design guidelines feels unnatural.

Dzmitry Huba
A: 

It's better to have a factory method pattern vs new keyword. The idea is to move complete instantiation of objects outside the business logic. This principle is the crux of dependency injection. And, the work of the factory method can be delegated to a Dependency Injection Framework like Spring.net or Castle Windsor at a later point.

AB Kolan
+1  A: 

Use the Abstract Factory pattern when a system should be independent of how its products are created, composed, and represented. a system should be configured with one of multiple families of products. a family of related product objects is designed to be used together, and you need to enforce this constraint. you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

Davit Siradeghyan
+2  A: 

Although this isn't necessarily it's primary use, it's good for something where you have specialized instances of a class:

public ITax BuildNewSalesTax()
public ITax BuildNewValueAddedTax()

You need both methods to build a tax object, but you don't want to have to depend on using "new" everytime because the constructors may be complex. This way I encapsulate all of the changes into a single method that is clear to others for future maintenance.

Austin
A: 

I think its when you want your application to be loosely coupled and extensible in future without coding changes.

I have written a post on blog as to why i choose the factory pattern in my project and may be it can give you more insight. The example is in PHP but i think its applicable in general to all languages.

http://www.mixedwaves.com/2009/02/implementing-factory-design-pattern/

Penuel
A: 

Hi

I am using Factory pattens when

  1. When a class does not know which class of objects it must create.

  2. A class specifies its sub-classes to specify which objects to create.

  3. In programmer’s language (very raw form), you can use factory pattern where you have to create an object of any one of sub-classes depending on the data provided.

Dhrumil Shah