简介
定义算法族,分别封装起来,让它们之间可相互替换,此模式让算法的变化独立于使用算法的客户
实际应用场景
账单同步的功能。来自不同平台的账单,使用不同的文件保存,需要解析文件,校验数据,保存数据,归档账单
UML
代码实现
/**
* 解析数据接口
*
* @author xiaoy
* @since 2021-3-8 15:33
*/
public interface IParsingData {
void parsingData();
}
/**
* 校验数据接口
*
* @author xiaoy
* @since 2021-3-8 15:37
*/
public interface ICheckData {
void checkData();
}
/**
* 保存数据接口
*
* @author xiaoy
* @since 2021-3-8 15:38
*/
public interface ISaveData {
void saveData();
}
/**
* 上传数据
*
* @author xiaoy
* @since 2021-3-8 15:39
*/
public interface IUploadData {
void uploadData();
}
/**
* @author xiaoy
* @since 2021-3-8 15:41
*/
public class ParsingDataA implements IParsingData {
@Override
public void parsingData() {
System.out.println("com.xiaoy.github.strategy.ParsingDataA.parsingData");
}
}
/**
* @author xiaoy
* @since 2021-3-8 15:41
*/
public class ParsingDataB implements IParsingData {
@Override
public void parsingData() {
System.out.println("com.xiaoy.github.strategy.ParsingDataB.parsingData");
}
}
/**
* @author xiaoy
* @since 2021-3-8 15:40
*/
public class CheckDataA implements ICheckData {
@Override
public void checkData() {
System.out.println("com.xiaoy.github.strategy.CheckDataA.checkData");
}
}
/**
* @author xiaoy
* @since 2021-3-8 15:42
*/
public class SaveDataA implements ISaveData {
@Override
public void saveData() {
System.out.println("com.xiaoy.github.strategy.SaveDataA.saveData");
}
}
/**
* @author xiaoy
* @since 2021-3-8 15:42
*/
public class UploadDataA implements IUploadData {
@Override
public void uploadData() {
System.out.println("com.xiaoy.github.strategy.UploadDataA.uploadData");
}
}
/**
* 账单数据同步
*
* @author xiaoy
* @since 2021-3-8 15:45
*/
public abstract class BillSynchronization {
protected String billNo;
private IParsingData parsingData;
private ICheckData checkData;
private ISaveData saveData;
private IUploadData uploadData;
public void parsingData() {
parsingData.parsingData();
}
public void checkData() {
checkData.checkData();
}
public void saveData() {
saveData.saveData();
}
public void uploadData() {
uploadData.uploadData();
}
public BillSynchronization setParsingData(IParsingData parsingData) {
this.parsingData = parsingData;
return this;
}
public BillSynchronization setCheckData(ICheckData checkData) {
this.checkData = checkData;
return this;
}
public BillSynchronization setSaveData(ISaveData saveData) {
this.saveData = saveData;
return this;
}
public BillSynchronization setUploadData(IUploadData uploadData) {
this.uploadData = uploadData;
return this;
}
}
/**
* A 类型的账单
*
* @author xiaoy
* @since 2021-3-8 15:51
*/
public class BillSynchronizationA extends BillSynchronization {
public BillSynchronizationA(String billNo) {
this.billNo = billNo;
}
}
/**
* @author xiaoy
* @since 2021-3-8 15:53
*/
public class Main {
public static void main(String[] args) {
BillSynchronizationA billSynchronizationA = new BillSynchronizationA("AAA");
billSynchronizationA.setCheckData(new CheckDataA())
.setParsingData(new ParsingDataA())
.setSaveData(new SaveDataA())
.setUploadData(new UploadDataA());
System.out.println(billSynchronizationA.billNo + " 开始执行:");
billSynchronizationA.parsingData();
billSynchronizationA.checkData();
billSynchronizationA.saveData();
billSynchronizationA.uploadData();
BillSynchronizationA billSynchronizationB = new BillSynchronizationA("BBB");
billSynchronizationB.setCheckData(new CheckDataA())
.setParsingData(new ParsingDataB())
.setSaveData(new SaveDataA())
.setUploadData(new UploadDataA());
System.out.println(billSynchronizationB.billNo + " 开始执行:");
billSynchronizationB.parsingData();
billSynchronizationB.checkData();
billSynchronizationB.saveData();
billSynchronizationB.uploadData();
}
}
测试
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/15393.html