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

推荐订阅源

博客园 - Franky
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
宝玉的分享
宝玉的分享
量子位
N
Netflix TechBlog - Medium
M
MIT News - Artificial intelligence
GbyAI
GbyAI
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
Y
Y Combinator Blog
L
LangChain Blog
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
U
Unit 42
Martin Fowler
Martin Fowler
aimingoo的专栏
aimingoo的专栏
G
Google Developers Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客

博客园 - 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、实现本模式时,要考虑拷贝对象状态的效率问题,如果对象开销比较大,可以采用某种增量式改变来改进本模式。