工厂模式的定义为:Define an interface for creating an object ,but let subclass decide which class to instantiate,Factory Methods lets a class defer instantiation to subclass.(定义一个用于创建对象的接口,让其子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到了其子类)。
工厂模式的UML为:
其java实现为:
productInf
/**
*
*/
package com.helloclq.factory;
/**
* @author chengliqun
*
*/
public interface ProductInf {
public void doSthA();
public void doSthB();
}
其两个实现,ProductA+ProductB分别为:
/**
*
*/
package com.helloclq.factory;
/**
* @author chengliqun
*
*/
public class ProductA implements ProductInf {
/* (non-Javadoc)
* @see com.helloclq.factory.ProductInf#doSthA()
*/
@Override
public void doSthA() {
// TODO Auto-generated method stub
System.out.println("ProductA-----------doSthA" );
}
/* (non-Javadoc)
* @see com.helloclq.factory.ProductInf#doSthB()
*/
@Override
public void doSthB() {
// TODO Auto-generated method stub
System.out.println("ProductA-----------doSthB" );
}
}
/**
*
*/
package com.helloclq.factory;
/**
* @author chengliqun
*
*/
public class ProductB implements ProductInf {
/* (non-Javadoc)
* @see com.helloclq.factory.ProductInf#doSthA()
*/
@Override
public void doSthA() {
// TODO Auto-generated method stub
System.out.println("ProductB-----------doSthA" );
}
/* (non-Javadoc)
* @see com.helloclq.factory.ProductInf#doSthB()
*/
@Override
public void doSthB() {
// TODO Auto-generated method stub
System.out.println("ProductB-----------doSthB" );
}
}
抽象工厂为:
/**
*
*/
package com.helloclq.factory;
/**
* @author chengliqun
*
*/
public abstract class AbsFactory {
public abstract T createProduct(Class c);
}
工厂子类为:
/**
*
*/
package com.helloclq.factory;
/**
* @author chengliqun
*
*/
public class ProductFactory extends AbsFactory {
/* (non-Javadoc)
* @see com.helloclq.factory.AbsFactory#createProduct(java.lang.Class)
*/
@Override
public T createProduct(Class c) {
// TODO Auto-generated method stub
ProductInf product = null;
try {
product = (ProductInf)Class.forName(c.getName()).newInstance();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (T) product;
}
}
测试如下:
package com.helloclq.factory;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AbsFactory factory = new ProductFactory();
ProductInf productA = factory.createProduct(ProductA.class);
productA.doSthA();
productA.doSthB();
}
}
输出结果为:
ProductA-----------doSthA
ProductA-----------doSthB
工厂模式有很多变形:
1.简单工厂模式
如果我们不需要很多工厂,一个模块就一个工厂,我们就没必要生产出工厂,直接用一个静态方法就能生产Product,这样就很简单了
/**
*
*/
package com.helloclq.factory;
/**
* @author chengliqun
*
*/
public class ProductFactory {
/* (non-Javadoc)
* @see com.helloclq.factory.AbsFactory#createProduct(java.lang.Class)
*/
@Override
public static T createProduct(Class c) {
// TODO Auto-generated method stub
ProductInf product = null;
try { product = (ProductInf)Class.forName(c.getName()).newInstance();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (T) product;
}
}
2.工厂容器
如果我们在工厂中放一个容器,用来保存生产出来的产品,这样的话,我们可以延迟产品类的释放,也可以设置要保存的最大数量,JDBC中的连接池就是这个原理弄出来的