PerKins Zhu

Le vent se lève,il faut tenter de vivre.
随笔 - 86, 文章 - 0, 评论 - 45, 阅读 - 21万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

设计模式—工厂方法模式

Posted on   PerKins.Zhu  阅读(363)  评论(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!
 *     ┃   ┗━━━┓
 *     ┃       ┣┓
 *     ┃       ┏┛
 *     ┗┓┓┏━┳┓┏┛
 *      ┃┫┫ ┃┫┫
 *      ┗┻┛ ┗┻┛
 * ━━━━━━神兽出没━━━━━━
复制代码

 

  

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示