PerKins Zhu

Le vent se lève,il faut tenter de vivre.

导航

设计模式—工厂方法模式

Posted on 2016-08-28 11:44  PerKins.Zhu  阅读(361)  评论(0编辑  收藏  举报

工厂方法模式让父类在不知道具体实现的情况下成自己功能的调用。实现方法是通过子类继承父类的抽象方法来获取被调用子类。(这里有些DI的味道)

    优势:1、可以在不知道具体调用和实现的情况下进行编程

       2、更加容易扩展新版本

    劣势:1、在客户端需要和具体的产品对象进行耦合,在调用处必须要知道需要调用的具体类。(可以组合简单工厂模式避免)

示例说明:  客户端要进行文件导出,文件的类型有多种,这里只简单的列出两种txt和DB类型。ExportFileOperator是不知道具体要对那种文件进行导出的,只

       有客户端在进行调用的时候才知掉要对哪种类型文件进行导出。

类图如下:

 

下面看具体实现代码:

ExportFileApi:定义所有文件的标准,必须有export(File file)方法。

package com.zpj.designMode.FactoryMethodPattern;

import java.io.File;

/**
 * @author PerKins Zhu
 * @date:2016年8月28日 上午10:10:46
 * @version :1.1
 * 
 */
public interface ExportFileApi {

    public void export(File file);
}

 

 ExportDBFile:DB文件导出实现类

package com.zpj.designMode.FactoryMethodPattern;

import java.io.File;

/**
 * @author PerKins Zhu
 * @date:2016年8月28日 上午10:12:35
 * @version :1.1
 * 
 */
public class ExportDBFile implements ExportFileApi {

    @Override
    public void export(File file) {
        System.out.println("-------开始导出DB文件---------");
    }

}

 

 ExportTxtFile:txt文件导出实现类

package com.zpj.designMode.FactoryMethodPattern;

import java.io.File;

/**
 * @author PerKins Zhu
 * @date:2016年8月28日 上午10:12:35
 * @version :1.1
 * 
 */
public class ExportTxtFile implements ExportFileApi {

    @Override
    public void export(File file) {
        System.out.println("-------开始导出txt文件---------");
    }

}

 ExportFileOperator:由客户端直接调用的导出操作类,在本来中不实现导出操作,由其子类具体实现。

package com.zpj.designMode.FactoryMethodPattern;

import java.io.File;

/**
 * @author PerKins Zhu
 * @date:2016年8月28日 上午10:14:25
 * @version :1.1
 * 
 */
public abstract class ExportFileOperator {

    public void export(File file){
        ExportFileApi efa = FactoryMethod();//获取需要导出的文件类型
        efa.export(file);//调用自身的export()进行导出操作
    }

    public abstract ExportFileApi FactoryMethod();
}

ExportDBFileOperator:继承public abstract ExportFileApi FactoryMethod();,向调用处返回ExportDBFile类型对象

package com.zpj.designMode.FactoryMethodPattern;
/**
 * @author PerKins Zhu
 * @date:2016年8月28日 上午10:18:46
 * @version :1.1
 * 
 */
public class ExportDBFileOperator extends ExportFileOperator{

    @Override
    public ExportFileApi FactoryMethod() {
        return new ExportDBFile();
    }

}

 

 ExportTxtFileOperator:继承public abstract ExportFileApi FactoryMethod();,向调用处返回ExportTxtFile类型对象

package com.zpj.designMode.FactoryMethodPattern;
/**
 * @author PerKins Zhu
 * @date:2016年8月28日 上午10:18:46
 * @version :1.1
 * 
 */
public class ExportTxtFileOperator extends ExportFileOperator{

    @Override
    public ExportFileApi FactoryMethod() {
        return new ExportTxtFile();
    }

}

 

 ClientTest:客户端进行调用导出文件。

package com.zpj.designMode.FactoryMethodPattern;

import java.io.File;

import org.junit.Test;

/**
 * @author PerKins Zhu
 * @date:2016年8月28日 上午10:21:50
 * @version :1.1
 * 
 */
public class ClientTest {

    File file = new File("G:\\test\\test.txt");
    
    //版本1测试
    @Test
    public void test01(){
        //这里可以根据需要导出的文件实例化相应的operator对象
        ExportFileOperator txtFileOperator = new ExportTxtFileOperator();
        txtFileOperator.export(file);
        
        ExportFileOperator DBFileOperator = new ExportDBFileOperator();
        DBFileOperator.export(file);
        
        //在这里可以和简单工厂模式进行组合,根据配置文件或者参数实例化不同的operator,见:http://www.cnblogs.com/PerkinsZhu/p/5813644.html
    }
}

 

 如果需要添加xml文件类型,需添加一个ExportFileApi的实现类 ExportXMLFile,实现自身的导出操作 export(File file);

            一个ExportFileOperator的子类ExportXMLFileOperator,实现ExportXMLFile的调用方法public ExportFileApi FactoryMethod() 。

这样在客户端处即可对xml文件进行调用。

 

如果在调用处整合工厂方法模式即可通过参数或者配置文件来降低耦合度。见:设计模式—简单工厂

   

*
 *----------Dragon be here!----------/
 *    ┏┓   ┏┓
 *   ┏┛┻━━━┛┻┓
 *   ┃       ┃
 *   ┃   ━   ┃
 *   ┃ ┳┛ ┗┳ ┃
 *   ┃       ┃
 *   ┃   ┻   ┃
 *   ┃       ┃
 *   ┗━┓   ┏━┛
 *     ┃   ┃神兽护体,Code is far away from bug with the animal protecting
 *     ┃   ┃代码无BUG!
 *     ┃   ┗━━━┓
 *     ┃       ┣┓
 *     ┃       ┏┛
 *     ┗┓┓┏━┳┓┏┛
 *      ┃┫┫ ┃┫┫
 *      ┗┻┛ ┗┻┛
 * ━━━━━━神兽出没━━━━━━