工厂方法模式

package com.fm.component;

public class Client {
	public static void main(String[] args) {
	    OperaterBiz ob = new OperaterByDb();
	    ob.exportData("测试数据");
	}
}

package com.fm.component;
import com.fm.service.DataExportService;

/**
 * 业务处理类,供客户端调用
 * @author admin
 *
 */
public abstract class OperaterBiz {
	public void exportData(String data) {// 共同业务逻辑处理
		System.out.println("---------------进行一系列业务处理");
		DataExportService des = factoryMethod();
		// 调用具体实现
		des.export(data);
	}
	// 由子类决定
	protected abstract DataExportService factoryMethod();

}


package com.fm.component;

import com.fm.service.DataExportService;
import com.fm.service.impl.DataExportByDb;

public class OperaterByDb extends OperaterBiz{

	@Override
	protected DataExportService factoryMethod() {
		return new DataExportByDb();
	}

}

package com.fm.component;

import com.fm.service.DataExportService;
import com.fm.service.impl.DataExportByFile;

public class OperaterByFile extends OperaterBiz{

	@Override
	protected DataExportService factoryMethod() {
		return new DataExportByFile();
	}

}

package com.fm.component;

import com.fm.service.DataExportService;
import com.fm.service.impl.DataExportByXML;

public class OperaterByXML extends OperaterBiz{

	@Override
	protected DataExportService factoryMethod() {
		return new DataExportByXML();
	}

}

package com.fm.service;

/**
 * 数据具体导出方式接口定义
 * @author admin
 *
 */
public interface DataExportService {
	
	public void export(String data);

}

package com.fm.service.impl;
import com.fm.service.DataExportService;
public class DataExportByDb implements DataExportService {
	@Override
	public void export(String data) {
	    System.out.println("通过DB的方式导出");
	}
}

package com.fm.service.impl;
import com.fm.service.DataExportService;
public class DataExportByFile implements DataExportService {
	@Override
	public void export(String data) {
		System.out.println("通过text的方式导出");
	}
}

package com.fm.service.impl;
import com.fm.service.DataExportService;
public class DataExportByXML implements DataExportService {

	@Override
	public void export(String data) {
		System.out.println("通过xml的方式导出");
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值