备忘录模式

简介: /* * * User: Administrator * Date: 2007-7-2 * Time: 9:02 */using System;using System.Collections.Generic;namespace myMemonto{    public interface IMemonto{}    public class State    {        publ
/*
 *
 * User: Administrator
 * Date: 2007-7-2
 * Time: 9:02
 
*/
using  System;
using  System.Collections.Generic;

namespace  myMemonto
{

    
public   interface  IMemonto{}

    
public   class  State
    {
        
public  State(){}
        
public   string  stateName; // 状态名
         public   int  Level; // 状态级别
    }

    
// 发起者
     public   class  Originator
    {
        
private   class  Memonto:IMemonto
        {
            
private  State _state;
            
public  Memonto(State st)
            {
                _state
= new  State();
                _state.stateName
= st.stateName;
                _state.Level
= st.Level;
            }
        
public  State state
        {
            
get {
                
return  _state;
            }
            
set {
                _state
= value;
            }
        }

        }




        
private  State state;

        
public  Originator()
        {
            state
= new  State();
            state.stateName
= " begin " ;
            state.Level
= 1 ;
        }
        
public   string  StateName
        {
            
get { return  state.stateName;}
            
set {state.stateName = value;}
        }
        
public   int  Level
        {
            
get
            {
                
return  state.Level;
            }
            
set
            {
                state.Level
= value;
            }
        }
        
public  IMemonto CreateMomento()    {
            
return   new  Memonto( this .state);
        }
        
public   void  SetState(IMemonto m)
        {
            Memonto mm
= (Memonto)m;
            
this .state = mm.state;
        }

    }

    
public   class  Caretaker:System.Collections.CollectionBase
    {
        
public  Caretaker(){}
        
public   int  Add(IMemonto st)
        {
            
return  List.Add(st);
        }
        
public  IMemonto  this [ int  index]{
            
get {
                
return  (IMemonto)List[index];
            }

            
set {
                List[index]
= value;
            }
        }
        
public   void  Remove(IMemonto st){
            List.Remove(st);
        }
        
public  IMemonto Restore()
        {
            
if (List.Count == 0 return   null ;
            IMemonto st
= (IMemonto) List[List.Count - 1 ];
            List.Remove(st);
            
return  st;
        }
    }



    
class  MainClass
    {
        
public   static   void  Main( string [] args)
        {
            
// Console.WriteLine("Hello World!");
            Originator o = new  Originator();
            Caretaker c
= new  Caretaker();
            c.Add(o.CreateMomento());

            o.StateName
= " one " ;
            o.Level
= 100 ;

            c.Add(o.CreateMomento());

            o.StateName
= " two " ;
            o.Level
= 200 ;

            c.Add(o.CreateMomento());

            IMemonto m
= c.Restore();

            
while (m != null ){
                o.SetState(m);
                System.Console.WriteLine(o.StateName);
                m
= c.Restore();
            }
            System.Console.ReadLine();
        }
    }
}
 
相关文章
|
4月前
|
设计模式 存储 Java
聊聊Java设计模式-备忘录模式
备忘录模式(Memento Design Pattern),也叫快照(Snapshot)模式。指在不违背封装原则前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便之后恢复对象为先前的状态。
38 0
聊聊Java设计模式-备忘录模式
|
5月前
|
存储 设计模式 Java
|
5月前
|
设计模式 安全 C++
行为型 备忘录模式
行为型 备忘录模式
23 1
|
7月前
|
设计模式 存储 数据库
设计模式~备忘录模式(memento)-22
备忘录模式(Memento Pattern)保存一个对象的某个状态,以便在适当的时候恢复对象。备忘录模式属于行为型模式。记录快照(瞬间状态)/存盘。 目录  (1)优点: (2)缺点: (3)使用场景: (4)注意事项: (5)应用实例: 代码
24 1
|
12月前
|
设计模式 存储 Java
Java设计模式-备忘录模式(Memento)
Java设计模式-备忘录模式(Memento)
|
设计模式 存储 Java
Java设计模式 ->备忘录模式
Java设计模式 ->备忘录模式
85 0
|
设计模式
我学会了,备忘录模式
备忘录模式属于行为型模式,这个类型的设计模式总结出了 类、对象之间的经典交互方式,将类、对象的行为和使用解耦了,花式的去使用对象的行为来完成特定场景下的功能。
125 0
我学会了,备忘录模式
|
uml
状态模式与备忘录模式(1)
状态模式与备忘录模式(1)
77 0
状态模式与备忘录模式(1)
|
存储 Java Spring
状态模式与备忘录模式(3)
状态模式与备忘录模式(3)
119 0
状态模式与备忘录模式(3)
|
存储 程序员 开发工具
状态模式与备忘录模式(2)
状态模式与备忘录模式(2)
121 0
状态模式与备忘录模式(2)