views:

57

answers:

6

Hi all,

Im having trouble getting my head around the concept of the factory design pattern. As far as i understand its to allow calling code not to have to worry about how an individual object is instantiated, simply to know that it will implement a particular interface.

I cant see how this saves any code though.

for instance, if i had 3 types of vehicle

lorry, car, van

and i created a class for each of them, i could just use a switch statement. Whereas with the examples of factory classes ive seen so far, i could have an interface:

Interface vehicle {
    method drive();
}

and then a class:

Class vehiclefac implements vehicle {
    method createvehicle(type) {
     // choose car type and return
}    
}

i still need to use a switch statement to pick my type of vehicle, further to this, if i want to add new cars, i still have to add them to the end of the switch statement and create an appropriate sub class to implement them.

If someone can clear up what im not getting id be very grateful, examples in python especially appreciated.

A: 

The factory is meant to encapsulate the switch logic. This means that you don't need to worry about how the vehicle is created everywhere you need a vehicle and you don't need to change code anywhere other than the factory when you add a new type of vehicle.

Brian Clements
A: 

Think of a factory as a "virtual constructor". Yes, you have to have a common interface, and that is the static type of the object returned, but you can create any type as long as they implement that common interface.

As for adding new subtypes, you can do it with reflection in languages that allow it. I don't know (yet) if Python is one of them.

duffymo
A: 

Have a look here: http://stackoverflow.com/questions/806911/is-this-factory-method-creation-pattern They dispute on your problem.

Gadolin
+1  A: 

It's not about reduction of code, it's more about dependency decoupling. If you code to an interface, and use a factory to retrieve your instances, you don't need to know what concrete class you're using. If later in the project someone writes an alternate implementation, the factory can be configured to use the new class without modifying the factory's clients.

For instance, imagine a factory that created stream objects for data serialization. An instance could request a stream interface to serialize itself to. Depending on configuration or state, the factory could be configured to give instances which write to the filesystem. In a different state/configuration, it could give out network stream implementations. The factory logic which selects the right implementation can be as simple or complex as the requirements need, but the client code doesn't need to know anything about the various implementations.

There isn't necessarily less code in the project to allow this, but the code is loosely coupled and much more easily maintained.

codelark
+1  A: 

The factory method pattern is a Creational pattern. It can be used to build objects of a particular base type with out specifying the exact concrete type, this is determined by any input and the create logic inside the factory.

public class VehicleFactory
{
     public IVehicle Create(int noOfWheels)
     {
        if (noOfWheels == 1)
            return new UniCycle();
        if (noOfWheels == 2)
            return new MotorCycle();
        if (noOfWheels == 3)
            return new Trike();
        if (noOfWheels == 4)
            return new Car();

        throw new NotImplementedException();
    }
}

public class UniCycle : IVehcicle { ... }
public class MotorCycle: IVehcicle { ... }
public class Trike: IVehcicle { ... }
public class Car: IVehcicle { ... }

The following is a simple C# example using your vehicle idea as the basis. The usefulness is that any consuming code doesn't care what the concrete class is just that it gets back an IVehicle. It doesn't have to be done with interfaces either, all though I prefer them, it could also be done with an abstract base class depending on the situation.

When I use the factory pattern I normally have the factory implement an interface, this is called the Abstract Factory Pattern. For this example it would be something like:

public interface IVehicleFactory
{
    IVehicle Create(int noOfWheels);
}

The factory can then be injected into classes (see IoC). All together it gives you:

  • Decouple your code.
  • Allows for different implementations of a factory.
  • Consuming code cleaner, no object instantiation.
  • Unit testing is easier, fewer complex concrete classes to worry about.

The wiki link I have provided for the Abstract Factory Pattern has some examples in Python.

Bronumski
A: 

The factory method pattern is a Creational pattern. It encapsulate the logic on what specialization of the same object hierarchy should be created for a particular case.

fabrizioM