备忘录模式

/*
 *
 * 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==0return 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();
        }
    }
}
 
上一篇:iOS获取和监测设备基本信息——UIDevice的使用


下一篇:访问者模式