备忘录模式

来源:互联网 发布:淘宝女鞋店推荐 编辑:程序博客网 时间:2024/04/30 04:58
/*
 *
 * 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();
        }
    }
}
 
原创粉丝点击