JAVA模式之工厂模式

工厂方法模式是类的创建模式,又叫做虚拟构造子(Virtual Constructor)模式或者多态性工厂(Polymorphic Factory)模式。

工厂方法模式的用意是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类中。

例如:导出的文件格式也可能有所不同,可能是HTML、CSV、PDF等。每种格式导出的结构有所不同,一种是标准结构,一种是财务需要的结构。

抽象工厂(ExportFactory)角色:担任这个角色的是工厂方法模式的核心,任何在模式中创建对象的工厂类必须实现这个接口。在实际的系统中,这个角色也常常使用抽象类实现。

具体工厂(ExportHtmlFactory、ExportPdfFactory)角色:担任这个角色的是实现了抽象工厂接口的具体JAVA类。具体工厂角色含有与业务密切相关的逻辑,并且受到使用者的调用以创建导出类(如:ExportStandardHtmlFile)。

抽象导出(ExportFile)角色:工厂方法模式所创建的对象的超类,也就是所有导出类的共同父类或共同拥有的接口。在实际的系统中,这个角色也常常使用抽象类实现。

具体导出(ExportStandardHtmlFile等)角色:这个角色实现了抽象导出(ExportFile)角色所声明的接口,工厂方法模式所创建的每一个对象都是某个具体导出角色的实例。

具体代码实现如下:

public interface ExportFactory {
public ExportFile factory(String type);
}
public class ExportHtmlFactory implements ExportFactory {
public ExportFile factory(String type) {
if("standard".equals(type)){
return new ExportStandardHtmlFile();
}else if("financial".equals(type)){
return new ExportFinancialHtmlFile();
}else{
throw new RuntimeException("没有找到对象");
}
}
}
public class ExportPdfFactory implements ExportFactory{
public ExportFile factory(String type) {
if("standard".equals(type)){
return new ExportStandardPdfFile();
}else if("financial".equals(type)){
return new ExportFinancialPdfFile();
}else{
throw new RuntimeException("没有找到对象");
}
}
}
-----------------------------------------------------------------------------
public interface ExportFile {
public boolean export(String data);
}
public class ExportStandardHtmlFile implements ExportFile {
public boolean export(String data) {
/**
* 业务逻辑
*/
System.out.println("导出标准HTML文件");
return true;
}
}
public class ExportFinancialHtmlFile implements ExportFile {
public boolean export(String data) {
/**
* 业务逻辑
*/
System.out.println("导出财务版HTML文件");
return true;
}
}
public class ExportStandardPdfFile implements ExportFile {
public boolean export(String data) {
/**
* 业务逻辑
*/
System.out.println("导出标准PDF文件");
return true;
}
}
public class ExportFinancialPdfFile implements ExportFile {
public boolean export(String data) {
/**
* 业务逻辑
*/
System.out.println("导出财务版PDF文件");
return true;
}
}
----------------------------------------------------------------------------
public class FactoryTest {
public static void main(String[] args) {
String data = "";
ExportFactory exportFactory = new ExportHtmlFactory();
ExportFile ef = exportFactory.factory("financial");
ef.export(data);
}
}

与简单工厂模式的对比:
1>
工厂方法模式和简单工厂模式在结构上的不同很明显。工厂方法模式的核心是一个抽象工厂类,而简单工厂模式把核心放在一个具体类上。

 


 2>工厂方法模式退化后可以变得很像简单工厂模式。设想如果非常确定一个系统只需要一个具体工厂类,那么不妨把抽象工厂类合并到具体工厂类中去。由于只有一个具体工厂类,所以不妨将工厂方法改为静态方法,这时候就得到了简单工厂模式。
public class ConvertToSimpleFactory {
static ExportFile exportFile;
public static ExportFile factory(String construct, String type){
if ("HTML".equals(construct)) {
if("standard".equals(type)){
exportFile = new ExportStandardHtmlFile();
}else if("financial".equals(type)){
exportFile = new ExportFinancialHtmlFile();
}else{
throw new RuntimeException("没有找到对象");
}
} else if ("PDF".equals(construct)) {
if("standard".equals(type)){
exportFile = new ExportStandardPdfFile();
}else if("financial".equals(type)){
exportFile = new ExportFinancialPdfFile();
}else{
throw new RuntimeException("没有找到对象");
}
}
return exportFile;
}
}
 public class FactoryTest {
public static void main(String[] args) {
String data = "";
ExportFile exportFile = ConvertToSimpleFactory.factory("HTML","financial");
exportFile.export(data);
}
}
  3>如果系统需要加入一个新的导出类型,那么所需要的就是向系统中加入一个这个导出类以及所对应的工厂类。没有必要修改客户端,也没有必要修改抽象工厂角色或者其他已有的具体工厂角色。对于增加新的导出类型而言,这个系统完全支持“开-闭原则”。

 
 
 
 
 
 
 

 

posted @ 2018-08-09 11:07  码农新手,快乐编码  阅读(344)  评论(0)    收藏  举报