设计模式之备忘录模式

一、概念

  • 官方:备忘录模式(Memento Pattern)保存一个对象的某个状态,以便在适当的时候恢复对象。备忘录模式属于行为型模式。
    个人理解:备忘录模式就是记录对象的状态,记录对象的属性。

二、场景

  • Git项目管理工具
  • 数据库事务源码
  • 浏览器[前进,后退]

三、实现

  • 条件
    • java
  • 场景
    • 以简单的项目管理为场景实现备忘录模式。
  • 代码实现
    • 解决方案类,类名:Solution
          package com.MemorandumPattern; 
          public class Solution {
              //类数量
              public int ClassCount;
              //代码大小
              public int CodeSize;
              //项目数量
              public int ProductCount;
              //保存当前对象状态
              public Memorandum SaveStateToMemorandum()
              {
                  return new Memorandum(ClassCount,CodeSize,ProductCount);
              }
              //恢复状态
              public  void  RecoveryStateToMemorandum(Memorandum memorandum)
              {
                  ClassCount = memorandum.ClassCount;
                  CodeSize = memorandum.CodeSize;
                  ProductCount = memorandum.ProductCount;
              }
          } 
      
    • 备忘录类,类名:Memorandum
          package com.MemorandumPattern; 
          public class Memorandum {
              //构造函数
              public Memorandum(int classCount,int codeSize,int productCount)
              {
                  ClassCount = classCount;
                  CodeSize = codeSize;
                  ProductCount = productCount;
              }
              //类数量
              public int ClassCount;
              //代码大小
              public int CodeSize;
              //项目数量
              public int ProductCount;
          } 
      
    • Git管理类,类名:Git
          package com.MemorandumPattern;
          
          import java.util.ArrayList;
          import java.util.List; 
          public class Git {
              private List<Memorandum> list = new ArrayList<>();
              public void Add(Memorandum memorandum)
              {
                  list.add(memorandum);
              }
              public Memorandum GetMemorandum(int  index)
              {
                  return list.get(index);
              }
          }
      
      
    • 入口函数类
          Git git = new Git(); 
          Solution solution = new Solution();
          solution.ClassCount =100;
          solution.CodeSize=120;
          solution.ProductCount =1 ;
          //记录当前对象的状态数据
          git.Add(solution.SaveStateToMemorandum()) ;
          Memorandum memorandum =git.GetMemorandum(0);
      
          solution.ClassCount =1000;
          solution.CodeSize=1200;
          solution.ProductCount =10 ;
          Memorandum memorandum1 =  solution.SaveStateToMemorandum();
           System.out.println("-------旧对象数据:"+memorandum.ClassCount+"---"+memorandum.CodeSize+"-----"+memorandum.ProductCount);
           System.out.println("-------新对象数据:"+memorandum1.ClassCount+"---"+memorandum1.CodeSize+"-----"+memorandum1.ProductCount);
           //恢复状态
           solution.RecoveryStateToMemorandum(memorandum);
           System.out.println("-------恢复状态:"+solution.ClassCount+"---"+solution.CodeSize+"-----"+solution.ProductCount);
      
      

四、优缺点

  • 优点
    • 给用户提供了一种可以恢复状态的机制,可以使用户能够比较方便地回到某个历史的状态。
    • 实现了信息的封装,使得用户不需要关心状态的保存细节。
  • 缺点
    • 消耗资源大,集合需要保存大量的备忘录对象,占用内存大。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值