惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
Engineering at Meta
Engineering at Meta
腾讯CDC
宝玉的分享
宝玉的分享
H
Help Net Security
I
InfoQ
博客园 - Franky
The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
博客园_首页
美团技术团队
Recent Announcements
Recent Announcements
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
The Cloudflare Blog
博客园 - 司徒正美
Vercel News
Vercel News
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
月光博客
月光博客

博客园 - Charly

我的电影记录 再去听他的演唱会 学友12月29日广州演唱会 张学友十大粤语&十大国语金曲赏析 妈妈的第二次重生 随便谈谈最近参与的2个项目 中奖的幸运与不幸 设计模式学习笔记二十五——总结 Moss2007搜索服务配置,没有索引器和搜索配置页面报错问题解决 设计模式学习笔记二十四——Visitor模式 设计模式学习笔记二十三——TemplateMethod模式 设计模式学习笔记二十二——Strategy模式 设计模式学习笔记二十一——State模式 设计模式学习笔记十九——Observer模式 在MOSS 2007中调试WebPart 设计模式学习笔记十八——Mediator模式 设计模式学习笔记十七——Iterator模式 设计模式学习笔记十六——Interpreter模式 设计模式学习笔记十五——Command模式
设计模式学习笔记二十——Memento模式
Charly · 2007-08-13 · via 博客园 - Charly

动机:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样就可以将该对象恢复到原先保存的状态。

场景:此处以图形系统为例,圆为原发器,该系统对圆进行处理,并保存可恢复状态序列。

结构图

Memento模式结构图

代码

namespace DesignPattern.Memento
{
    
/// <summary>
    
/// 原发器
    
/// </summary>

    public class Round 
    
{
        
private Point centerPoint;
        
private int radius;

        
public Round(Point centerPoint, int radius)
        
{
            
this.centerPoint = centerPoint;
            
this.radius = radius;
        }


        
public RoundMemento CreateMemento()
        
{
            RoundMemento memento 
= new RoundMemento();
            memento.SetState(
this.centerPoint, this.radius);
            
return memento;
        }


        
public void SetMemento(RoundMemento memento)
        
{
            
this.centerPoint = memento.centerPoint;
            
this.radius = memento.radius;
        }


        
public void Move(Point point)
        
{
        }


        
public void SetLength(int radius)
        
{
        }

    }


    
/// <summary>
    
/// 备忘录
    
/// </summary>

    public class RoundMemento
    
{
        
internal Point centerPoint;
        
internal int radius;

        
public RoundMemento()
        
{
        }


        
internal void SetState(Point centerPoint, int radius)
        
{
            
this.centerPoint = centerPoint;
            
this.radius = radius;
        }

    }

}

namespace DesignPattern.Memento
{
    
public class GraphicsSystem
    
{
        Round round 
= new Round (new Point(), 10);
        IList
<RoundMemento> mementos;

        
public GraphicsSystem()
        
{
            mementos 
= new List<RoundMemento>();
        }


        
public void Process()
        
{
            mementos.Add(round.CreateMemento());
            
// 操作圆
        }


        
public void Undo(int index)
        
{
            round.SetMemento(mementos[index]);
        }

    }

}

要点
        1、备忘录(Memento)存储原发器(Originator)对象的内部状态,在需要时恢复原发器状态。本模式适用于“由原发器管理,却又必须存储在原发器之外的信息”。
        2、实现本模式时,要防止原发器以外的对象访问备忘录对象。备忘录对象有两个接口,一个为原发器使用的宽接口,一个为其他对象使用的窄接口。
        3、实现本模式时,要考虑拷贝对象状态的效率问题,如果对象开销比较大,可以采用某种增量式改变来改进本模式。