Simple Factory Pattern


The Factory Design Pattern is probably the most used design pattern in modern programming languages like Java and C#. It comes in different variants and implementations. If you are searching for it, most likely, you’ll find references about the GoF patterns: Factory Method and Abstract Factory.

Use when ?

  • creates objects without exposing the instantiation logic to the client.
  • refers to the newly created object through a common interface

I will use example’s Head First Design Pattern

d1

The class PizzaStore our original store. have method orderPizza as:

public Pizza orderPizza(String code){
		Pizza pizza = null;

		if(code.equals("1001")){
			pizza = new SeafoodPizza();
		}else if(code.equals("1002")){
			pizza = new ShrimpScampiPizza();
		}else if(code.equals("1003")){
			pizza = new SupremePizza();
		}// can have multi-pizza at here

		pizza.prepare();
		pizza.bake();
		pizza.cut();
		pizza.box();

		return pizza;
	}

Encapsulating object creation

So now we know we’d be better off moving the object creation out of the orderPizza() method.  But how?  Well, what  we’re going to do is take the creation code and move it out into another object that is only going to be concerned with creating pizzas.We’ve got a name for this new object: we call it a Factory.

This is class PizzaFactory.java

public class PizzaFactory {
	public Pizza createPizza(String code){
		Pizza pizza = null;
		if(code.equals("1001")){
			pizza = new SeafoodPizza();
		}else if(code.equals("1002")){
			pizza = new ShrimpScampiPizza();
		}else if(code.equals("1003")){
			pizza = new SupremePizza();
		}
		return pizza;
	}
}

Client PizzaStore.java will fixed as :

public class PizzaStore {
	// We give PizzaStore a reference to a PizzaFactory
	PizzaFactory pizzaFactory = null;
	// PizzaStore gets the factory passed to it in constructor
	public PizzaStore(PizzaFactory pizzaFactory) {
		this.pizzaFactory = pizzaFactory;
	}

	public Pizza orderPizza(String code){
		Pizza pizza = null;
		// the orderPizza method uses the factory to create it's Pizza
		// by passing on the code of the order
		/**
		 * Notice : We're replaced the new operator with a method on the factory object
		 * 			No more concrete instantiations here !!
		 */
		pizza = pizzaFactory.createPizza(code);

		pizza.prepare();
		pizza.bake();
		pizza.cut();
		pizza.box();

		return pizza;
	}

}

 

 

The implementation is really simple

  • The client needs a product, but instead of creating it directly using the new operator, it asks the factory object for a new product, providing the information about the type of object it needs.
  • The factory instantiates a new concrete product and then returns to the client the newly created product(casted to abstract product class).
  • The client uses the products as abstract products without being aware about their concrete implementation.

Notice (Head First Design Pattern)

The simple factory isn’t actually a Design pattern. But it is commonly used. Some developers do mistake this idiom for the ” Factory Pattern

Class Registration – using reflection

If you can use reflection, for example in Java or .NET languages, you can register new product classes to the factory without even changing the factory itself. For creating objects inside the factory class without knowing the object type we keep a map between the productID and the class type of the product. In this case when a new product is added to the application it has to be registered to the factory. This operation doesn’t require any change in the factory class code.

Ebook HFDP define:

d1

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: