design-step-18
|——src
|——main
|--java
|--com.lino.design
|--Admin.java
|--ConfigFile.java
|--ConfigMemento.java
|--ConfigOriginator.java
|--test
|--java
|--com.lino.design.test
|--ApiTest.java
ConfigFile
以外,只新增加了三个类。
ConfigFile.java
package com.lino.design;
import java.util.Date;
/**
* @description: 配置文件
*/
public class ConfigFile {
/**
* 版本号
*/
private String versionNo;
/**
* 内容
*/
private String content;
/**
* 时间
*/
private Date dateTime;
/**
* 操作人
*/
private String operator;
public ConfigFile(String versionNo, String content, Date dateTime, String operator) {
this.versionNo = versionNo;
this.content = content;
this.dateTime = dateTime;
this.operator = operator;
}
public String getVersionNo() {
return versionNo;
}
public void setVersionNo(String versionNo) {
this.versionNo = versionNo;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getDateTime() {
return dateTime;
}
public void setDateTime(Date dateTime) {
this.dateTime = dateTime;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
}
ConfigMemento.java
package com.lino.design;
/**
* @description: 备忘录
*/
public class ConfigMemento {
private ConfigFile configFile;
public ConfigMemento(ConfigFile configFile) {
this.configFile = configFile;
}
public ConfigFile getConfigFile() {
return configFile;
}
public void setConfigFile(ConfigFile configFile) {
this.configFile = configFile;
}
}
ConfigOriginator.java
package com.lino.design;
/**
* @description: 记录者
*/
public class ConfigOriginator {
private ConfigFile configFile;
public ConfigFile getConfigFile() {
return configFile;
}
public void setConfigFile(ConfigFile configFile) {
this.configFile = configFile;
}
public ConfigMemento saveMemento() {
return new ConfigMemento(configFile);
}
public void getMemento(ConfigMemento configMemento) {
this.configFile = configMemento.getConfigFile();
}
}
saveMemento()
、获取 getMemento(ConfigMemento configMemento)
。
saveMemento()
:保存备忘录的时候会创建一个备忘录信息,并返回回去,交给管理者处理。getMemento
:获取之后并不是直接返回,而是把备忘录的信息交给现在的配置文件 this.configFile
。Admin.java
package com.lino.design;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @description: 管理员
*/
public class Admin {
private int cursorIdx = 0;
private List<ConfigMemento> mementoList = new ArrayList<>();
private Map<String, ConfigMemento> mementoMap = new HashMap<>(16);
public void append(ConfigMemento memento) {
mementoList.add(memento);
mementoMap.put(memento.getConfigFile().getVersionNo(), memento);
cursorIdx++;
}
public ConfigMemento undo() {
if (--cursorIdx <= 0) {
return mementoList.get(0);
}
return mementoList.get(cursorIdx);
}
public ConfigMemento redo() {
if (++cursorIdx > mementoList.size()) {
return mementoList.get(mementoList.size() - 1);
}
return mementoList.get(cursorIdx);
}
public ConfigMemento get(String versionNo) {
return mementoMap.get(versionNo);
}
}
List
、Map
。append()
、回滚 undo()
、返回 redo()
、定向获取 get(String versionNo)
。ApiTest.java
package com.lino.design.test;
import com.alibaba.fastjson.JSON;
import com.lino.design.Admin;
import com.lino.design.ConfigFile;
import com.lino.design.ConfigOriginator;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
/**
* @description: 单元测试
*/
public class ApiTest {
private Logger logger = LoggerFactory.getLogger(ApiTest.class);
@Test
public void test() {
Admin admin = new Admin();
ConfigOriginator configOriginator = new ConfigOriginator();
configOriginator.setConfigFile(new ConfigFile("100001", "配置内容A=哈哈", new Date(), "小零哥"));
admin.append(configOriginator.saveMemento());
configOriginator.setConfigFile(new ConfigFile("100002", "配置内容A=嘻嘻", new Date(), "小零哥"));
admin.append(configOriginator.saveMemento());
configOriginator.setConfigFile(new ConfigFile("100003", "配置内容A=默默", new Date(), "小零哥"));
admin.append(configOriginator.saveMemento());
configOriginator.setConfigFile(new ConfigFile("100004", "配置内容A=嘿嘿", new Date(), "小零哥"));
admin.append(configOriginator.saveMemento());
// 历史配置(回滚)
configOriginator.getMemento(admin.undo());
logger.info("历史配置(回滚)undo:{}", JSON.toJSONString(configOriginator.getConfigFile()));
// 历史配置(回滚)
configOriginator.getMemento(admin.undo());
logger.info("历史配置(回滚)undo:{}", JSON.toJSONString(configOriginator.getConfigFile()));
// 历史配置(前进)
configOriginator.getMemento(admin.redo());
logger.info("历史配置(前进)redo:{}", JSON.toJSONString(configOriginator.getConfigFile()));
// 历史配置(获取)
configOriginator.getMemento(admin.get("100002"));
logger.info("历史配置(获取)get:{}", JSON.toJSONString(configOriginator.getConfigFile()));
}
}
回滚1次
,再回滚1次
,之后向前进1次
,最后是获取指定的版本配置
。测试结果
17:04:48.903 [main] INFO com.lino.design.test.ApiTest - 历史配置(回滚)undo:{"content":"配置内容A=嘿嘿","dateTime":1675415088777,"operator":"小零哥","versionNo":"100004"}
17:04:48.913 [main] INFO com.lino.design.test.ApiTest - 历史配置(回滚)undo:{"content":"配置内容A=默默","dateTime":1675415088777,"operator":"小零哥","versionNo":"100003"}
17:04:48.913 [main] INFO com.lino.design.test.ApiTest - 历史配置(前进)redo:{"content":"配置内容A=嘿嘿","dateTime":1675415088777,"operator":"小零哥","versionNo":"100004"}
17:04:48.913 [main] INFO com.lino.design.test.ApiTest - 历史配置(获取)get:{"content":"配置内容A=嘻嘻","dateTime":1675415088777,"operator":"小零哥","versionNo":"100002"}