Simple Factory Pattern, Factory Method,and Abstract Factory Pattern

The Simple Factory Pattern is just a factory that returns a object of specified type, as simple as that. The UML diagram

UML diagram for simple factory pattern

The java code for example

public class ProductFactory{
	public Product createProduct(int productId){
		if (productId==ID1)
			return new OneProduct();
		if (productId==ID2) return
			return new AnotherProduct();
		... // so on for the other Ids
		
        return null; //if the id doesn't have any of the expected values
    }
    ...
}

This is a simple factory method, but it can get more complex. The above example does serve the function alright but it does not satisfy the open-close principle.

So there is another method called Class Registration that will satisfy the open-close principle – we do not need to change the factory class when we want to add new product class. The idea is have a register method that can register the class (using reflection or creator method enforced in product interface). But usually the factory method is a more preferable solution:

Factory Method:
The following is quoted from Gang of four:
“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”

The UML diagram:

The main difference between factory method and simple factory pattern, the caller in the factory method is not referencing the factory (creator) itself, but an abstract class the factory (creator) class inherited from, like factory f1 = new concreteFactory(); the factory method is defined in the concrete class and the concrete factory will have its own method to create a concrete product and return it to the caller (the caller is using an interface or parent class to access the product too). The following diagram shows the relationship:

Factory Method Diagram

When dealing with factory method, the contructor always declared private so users are forced to use the factory method. The factory method sometimes is declared static so it can limit the number of instances that share by different method.

Abstract Factory:
The big difference between abstract factory and above two is that by its own definition, an Abstract Factory is used to create a family of related products (Factory Method creates one product). In other words, the Abstract Factory is a super-factory which creates other factories (Factory of factories). Here is an example of Abstract factory

Abstract Factory diagram

Look & Feel Abstract Factory is the most common example. For example, a GUI framework should support several look and feel themes, such as Motif and Windows look. Each style defines different looks and behaviors for each type of controls: Buttons and Edit Boxes. In order to avoid the hardociding it for each type of control we define an abstract class LookAndFeel. This calls will instantiate, depending on a configuration parameter in the application one of the concrete factories: WindowsLookAndFeel or MotifLookAndFeel. Each request for a new object will be delegated to the instatiated concrete factory which will return the controls with the specific flavor.

Notice that the factory method can be used when concrete factory creates the concrete product.

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