List<Map<String, Object>> prrMapList = new LinkedList<>(); List<Map<String, Object>> ptrMapList = new LinkedList<>(); for (Record record : container) { if (record.getType().equalsIgnoreCase("PRR")) { Byte partFlg = (Byte) record.getData().getField("PART_FLG"); Map<String, Object> prrMap = new HashMap<>(); prrMap.put("lot","000000"); prrMap.put("product","STAR000000"); prrMap.put("process","test"); prrMap.put("test_time",new Date()); prrMap.put("wafer","000000"); prrMap.put("y_axis",record.getData().getField("Y_COORD")); prrMap.put("create_time",new Date()); prrMap.put("x_axis",record.getData().getField("X_COORD")); prrMap.put("user_name",userName); prrMap.put("soft_bin",record.getData().getField("SOFT_BIN")); prrMap.put("test_failed", partFlg == 0 ? 0 : 1); prrMap.put("hard_bin",record.getData().getField("HARD_BIN")); prrMapList.add(prrMap); } if (record.getType().equalsIgnoreCase("PTR")){ //Byte partFlg = (Byte) record.getData().getField("PART_FLG"); Map<String, Object> ptrMap = new HashMap<>(); ptrMap.put("product","STAR000000"); ptrMap.put("head_num",record.getData().getField("HEAD_NUM")); ptrMap.put("y_axis","");//需要找到来源 ptrMap.put("create_time",new Date()); ptrMap.put("x_axis","");//需要找到来源 ptrMap.put("user_name",userName); ptrMap.put("site_num",record.getData().getField("SITE_NUM")); //ptrMap.put("test_failed", partFlg == 0 ? 0 : 1); ptrMap.put("lot","000000"); ptrMap.put("result",record.getData().getField("RESULT")); ptrMap.put("test_time",new Date()); ptrMap.put("wafer","000000"); ptrMap.put("low_limit",record.getData().getField("LO_LIMIT")); ptrMap.put("high_limit",record.getData().getField("HI_LIMIT")); ptrMap.put("test_name",record.getData().getField("TEST_TXT")); ptrMap.put("test_number",record.getData().getField("TEST_NUM")); ptrMapList.add(ptrMap); } } 帮我用装饰者模式解偶
时间: 2024-03-05 11:51:46 浏览: 54
为了解耦,我们可以使用装饰者模式,将不同类型的记录处理逻辑抽象成一个接口,然后用不同的装饰者来实现该接口,最终将处理逻辑与原始代码解耦。
首先,我们创建一个 RecordProcessor 接口,用于处理记录:
```
public interface RecordProcessor {
void processRecord(Record record, List<Map<String, Object>> mapList, String userName);
}
```
然后,我们创建两个装饰者,分别用于处理 PRR 和 PTR 记录:
```
public class PrrRecordProcessor implements RecordProcessor {
@Override
public void processRecord(Record record, List<Map<String, Object>> mapList, String userName) {
Byte partFlg = (Byte) record.getData().getField("PART_FLG");
Map<String, Object> prrMap = new HashMap<>();
prrMap.put("lot","000000");
prrMap.put("product","STAR000000");
prrMap.put("process","test");
prrMap.put("test_time",new Date());
prrMap.put("wafer","000000");
prrMap.put("y_axis",record.getData().getField("Y_COORD"));
prrMap.put("create_time",new Date());
prrMap.put("x_axis",record.getData().getField("X_COORD"));
prrMap.put("user_name",userName);
prrMap.put("soft_bin",record.getData().getField("SOFT_BIN"));
prrMap.put("test_failed", partFlg == 0 ? 0 : 1);
prrMap.put("hard_bin",record.getData().getField("HARD_BIN"));
mapList.add(prrMap);
}
}
public class PtrRecordProcessor implements RecordProcessor {
@Override
public void processRecord(Record record, List<Map<String, Object>> mapList, String userName) {
Map<String, Object> ptrMap = new HashMap<>();
ptrMap.put("product","STAR000000");
ptrMap.put("head_num",record.getData().getField("HEAD_NUM"));
ptrMap.put("y_axis","");//需要找到来源
ptrMap.put("create_time",new Date());
ptrMap.put("x_axis","");//需要找到来源
ptrMap.put("user_name",userName);
ptrMap.put("site_num",record.getData().getField("SITE_NUM"));
//ptrMap.put("test_failed", partFlg == 0 ? 0 : 1);
ptrMap.put("lot","000000");
ptrMap.put("result",record.getData().getField("RESULT"));
ptrMap.put("test_time",new Date());
ptrMap.put("wafer","000000");
ptrMap.put("low_limit",record.getData().getField("LO_LIMIT"));
ptrMap.put("high_limit",record.getData().getField("HI_LIMIT"));
ptrMap.put("test_name",record.getData().getField("TEST_TXT"));
ptrMap.put("test_number",record.getData().getField("TEST_NUM"));
mapList.add(ptrMap);
}
}
```
最后,我们修改原始代码,将记录处理逻辑抽象成 RecordProcessor 接口,并使用装饰者来处理不同类型的记录:
```
List<Map<String, Object>> prrMapList = new LinkedList<>();
List<Map<String, Object>> ptrMapList = new LinkedList<>();
for (Record record : container) {
RecordProcessor processor;
if (record.getType().equalsIgnoreCase("PRR")) {
processor = new PrrRecordProcessor();
} else if (record.getType().equalsIgnoreCase("PTR")) {
processor = new PtrRecordProcessor();
} else {
continue;
}
processor.processRecord(record, prrMapList, userName);
}
```
阅读全文