Factory method pattern definition:

The Factory method pattern defines an interface for creating and objects, but lets subclasses to decide which class they will create.


I will try to present this pattern on a simple example. Imagine you work in a company that sales software. However, this software is, due to different laws, different for each country/region it is sold in (in our case EU and USA). You have two types of software (Pro and Basic that differ by price and package). You will need to create a single store that will allow users to only buy products from their country.

First, you can create an abstract class that will represent a product.

As you can see, the class itself is pretty simple. It contains a member variable for product name and has two methods that will prepare a package and print it’s price.
Now (as you need two product types for two countries) you need to create 4 product classes that will inherit from CSoftware class. I will only show you both EU classes as others are pretty self explanatory.

There. You are done with products. Now, you need two stores, that will create desired products. But first, you should create an abstract store class.

As you can see, createSoftwarePackage method is empty. This is because createSoftwarePackage differs from store to store. Method orderSoftware, though, is the same for all stores and can be implemented in abstract class.
Now, a source of both stores that inherit from CSoftwareStore.

Each store creates and returns an object depending on the type we wanted.

But what will that bring me?

You will only have to know which store you need to use and not care about the product returned. That is a job of each separate store. Thus, your code will be easier to read and to maintain.
Pasted below is a sample execution code.

The agent creates both stores and then for each store orders a software. Printed results should be: