徒手撸设计模式-备忘录模式

2022/7/1 6:49:31

本文主要是介绍徒手撸设计模式-备忘录模式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

概念

 

参考链接: https://www.runoob.com/design-pattern/memento-pattern.html

代码案例

新增生产备忘录类,并存状态属性

@Setter
@Getter
public class MemoProduction {
    public MemoProduction(String state) {
        this.state = state;
    }

    private String state;

}

 

备忘录状态类-可以报错备忘录状态和获取状态

@Setter
@Getter
@Slf4j
public class ProductionStatus {
    private String status;

    /**
     * 保存原始状态
     * @return
     */
    public MemoProduction saveOriginalStatus(){
        log.info("save original status is: {}",status);
        return new MemoProduction(status);
    }

    /**
     * 获取原始状态
     * @param memoProduction
     */
    public void getOriginalStatus(MemoProduction memoProduction){
        status= memoProduction.getState();
        log.info("get original status is: {}",status);
    }
}

 

备忘录容器

/**
 * 备忘录容器
 */
public class MemoContainer {
    private List<MemoProduction> memoProductionList= new ManagedList<>();

    public void add(MemoProduction memo){
        memoProductionList.add(memo);
    }

    public MemoProduction get(int index) {
        return memoProductionList.get(index);
    }
}

 

测试主类

/**
 * 设计模式控制器
 */
@RestController
@RequestMapping("/designPattern")
@Slf4j
public class DesignController {

    @PostMapping("/memo")
    public ResponseModel memo(@RequestBody String[] status) {
        log.info("memo   ---- start ");
        List list = new ArrayList();
        ProductionStatus productionStatus = new ProductionStatus();
        MemoContainer memoContainer = new MemoContainer();
        productionStatus.setStatus(status[0]);
        productionStatus.setStatus(status[1]);
        //创建备忘录,放入容器,此时状态为1
        memoContainer.add(productionStatus.saveOriginalStatus());
        productionStatus.setStatus(status[2]);
        //创建备忘录,放入容器,此时状态为2
        memoContainer.add(productionStatus.saveOriginalStatus());
        //设置备忘录状态为3
        productionStatus.setStatus(status[3]);
        //获取当前备忘录状态
        log.info("current status is: {}", productionStatus.getStatus());
        list.add("current status is: " + productionStatus.getStatus());
        //获取容器中第一个元素状态
        productionStatus.getOriginalStatus(memoContainer.get(0));
        log.info("current status is: {}", productionStatus.getStatus());
        list.add("current status is: " + productionStatus.getStatus());
        //获取容器中第二个元素状态
        productionStatus.getOriginalStatus(memoContainer.get(1));
        log.info("current status is: {}", productionStatus.getStatus());
        list.add("current status is: " + productionStatus.getStatus());
        log.info("memo   ---- end ");
        return new ResponseModel("备忘录模式完成", 200, list);
    }
}

 

测试案例

 

 

2022-07-01 01:21:51.537 INFO  memo   ---- start  【http-nio-8081-exec-3】【DesignController:80】
2022-07-01 01:21:51.537 INFO  save original status is: 1 【http-nio-8081-exec-3】【ProductionStatus:18】
2022-07-01 01:21:51.538 INFO  save original status is: 2 【http-nio-8081-exec-3】【ProductionStatus:18】
2022-07-01 01:21:51.538 INFO  current status is: 3 【http-nio-8081-exec-3】【DesignController:90】
2022-07-01 01:21:51.538 INFO  get original status is: 1 【http-nio-8081-exec-3】【ProductionStatus:28】
2022-07-01 01:21:51.539 INFO  current status is: 1 【http-nio-8081-exec-3】【DesignController:93】
2022-07-01 01:21:51.539 INFO  get original status is: 2 【http-nio-8081-exec-3】【ProductionStatus:28】
2022-07-01 01:21:51.542 INFO  current status is: 2 【http-nio-8081-exec-3】【DesignController:96】
2022-07-01 01:21:51.542 INFO  memo   ---- end  【http-nio-8081-exec-3】【DesignController:98】

 


                   

这篇关于徒手撸设计模式-备忘录模式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程