Define an interface for creating an object,but let subclasses decide which class to instantiate.Factory Method lets a class defer instantiation to subclass.

package com.design_patterns.www.factory_method;

import com.design_patterns.www.product.Product;

/**
 * 工厂方法核心抽象类
 * @author Administrator
 *
 */
public abstract class Creator {
	public abstract <T extends Product> T createProduct(Class<T> c);
}

ConcreteCreator.java

package com.design_patterns.www.factory_method.impl;
/**
 * 抽象工厂的实现类
 */
import com.design_patterns.www.factory_method.Creator;
import com.design_patterns.www.product.Product;

public class ConcreteCreator extends Creator {

	@Override
	public <T extends Product> T createProduct(Class<T> c) {
		// TODO Auto-generated method stub
		Product mProduct = null;
		try {
			mProduct = (Product) Class.forName(c.getName()).newInstance();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (T) mProduct;
	}

}

Product.java

package com.design_patterns.www.product;
/**
 * 产品接口
 * @author Administrator
 *
 */
public interface Product {
	public void sayHello();
}

ConcreteProduct1.java

package com.design_patterns.www.product;
/**
 * 产品接口
 * @author Administrator
 *
 */
public interface Product {
	public void sayHello();
}

另外还有ConcreteProduct1.java ConcreteProduct2.java两个类,跟ConcreteProduct1.java一样
Client.java

package com.design_patterns.www.client;

import com.design_patterns.www.factory_method.impl.ConcreteCreator;
import com.design_patterns.www.product.Product;
import com.design_patterns.www.product.impl.ConcreteProduct1;
import com.design_patterns.www.product.impl.ConcreteProduct2;
import com.design_patterns.www.product.impl.ConcreteProduct3;

public class Client {

	/**
	 * 场景类
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ConcreteCreator mConcreteCreator = new ConcreteCreator();
		
		Product concreteProduct1= null;
		concreteProduct1 = mConcreteCreator.createProduct(ConcreteProduct1.class);
		concreteProduct1.sayHello();
		
		Product concreteProduct2= null;
		concreteProduct2 = mConcreteCreator.createProduct(ConcreteProduct2.class);
		concreteProduct2.sayHello();
		
		Product concreteProduct3= null;
		concreteProduct3 = mConcreteCreator.createProduct(ConcreteProduct3.class);
		concreteProduct3.sayHello();
	}

}