C # Design Patterns: memo mode (Memento Pattern)

A, C # Design Patterns: memo mode (Memento Pattern)

1. Sponsor's role (Originator): record the internal state of the current moment, is responsible for creating and restoring memo data. Responsible for creating a memorandum of Memento, the current time to record their own internal states, and can be used to restore internal memo states. Originator Memento [sponsor] [memo] may decide according to what needs to be stored inside their own state.
2, memorandum role (Memento): responsible for internal state storage sponsor objects to provide state sponsor during recovery needs, and can prevent access to other objects other than memo Originator. Memorandum has two interfaces: Caretaker [management roles can only see the memorandum of narrow interface, he can only pass the memo to other objects. Originator promoters [] but it can be seen memo wide interface, which allows access to all data prior to return to the desired state.
3, the manager role (Caretaker): responsible for saving memo objects. Responsible memorandum Memento, Memento content can not be accessed or operations.

Second, the code

the using the System;
 the using the System.Collections.Generic;
 the using the System.Linq;
 the using the System.Text;
 the using the System.Threading;
 the using System.Threading.Tasks; 

namespace _22 memo mode. 
{ 
    
    ///  <Summary> 
    /// sector - data to be backed up, the status data is not operating
     ///  </ Summary> 
    public  Sealed  class Dept's 
    { 
        ///  <Summary> 
        /// sector
         ///  </ Summary> 
        public  String the DeptName { GET ; SET ;} 

        / // <Summary> 
        /// number
         ///  </ Summary> 
        public  String Number The { GET ; SET ;} 
    } 

    ///  <Summary> 
    /// promoters - [sponsor roles equivalent Originator
     ///  </ Summary> 
    public  Sealed  class DeptBackOriginator 
    { 
        // promoters need to save the internal state 
        Private List <Dept's> _deptList; 

        public List <Dept's> DeptList 
        { 
            GET 
            { 
                return  the this ._deptList; 
            } 

            SET 
            {
                the this ._deptList = value; 
            } 
        } 
        ///  <Summary> 
        /// sector initialization requires backup
         ///  </ Summary> 
        ///  <param name = "deptList"> </ param> 
        public DeptBackOriginator (List <Dept's > deptList) 
        { 
            IF (deptList =! null ) 
            { 
                the this ._deptList = deptList; 
            } 
            the else 
            { 
                the throw  new new ArgumentNullException The ( " parameter can not be empty! " ); 
            } 
        } 

        /// <Summary> 
        /// create memo object instance, to save the current period to save the memo contact list objects
         ///  </ Summary> 
        ///  <Returns> </ Returns> 
        public DeptMemento CreateMemento () 
        { 
            return  new new DeptMemento ( new new list <Dept's> ( the this ._deptList)); 
        } 

        ///  <Summary> 
        /// restore the data backed up to the department memo list
         ///  </ Summary> 
        ///  <param name = "Memento "> </ param> 
        public  void RestoreMemento (DeptMemento Memento) 
        { 
            the this .DeptList = memento.DeptListBack;
        }
        /// <Summary> 
        /// display data contained in the backup
         ///  </ Summary> 
        public  void the Show () 
        { 
            Console.WriteLine ( " sector list, a total of {0} sector, they are: " , DeptList.Count);
             the foreach (Dept's P in DeptList) 
            { 
                Console.WriteLine ( " sector: number {0}: {}. 1 " , p.DeptName, p.Number); 
            } 
        } 
    } 

    ///  <Summary> 
    /// memo object, for save state data, the object was the preservation of the specific status of data - the equivalent of [memorandum roles Memeto
     ///  </ the Summary> 
    public  Sealed class DeptMemento 
    { 
        ///  <Summary> 
        /// Save promoters sector data created
         ///  </ Summary> 
        public List <Dept's> DeptListBack { GET ; Private  SET ;} 

        public DeptMemento (List <Dept's> deptList) 
        { 
            DeptListBack = deptList; 
        } 
    } 

    ///  <Summary> 
    /// management role, it can manage the object] [memo, if a plurality of stored objects] [memo, of course, can add, delete and the like of the storage management processing object - - [manager equivalent roles the Caretaker
     ///  </ Summary> 
    public  Sealed  class MementoCaretaker 
    { 
        /// <Summary> 
        /// If you want to save the memo [plurality] objects, or to store a dictionary stack
         ///  </ Summary> 
        public the Dictionary < String , DeptMemento> DeptMementoDictionary { GET ; SET ;}
         ///  <Summary> 
        / // single backup point
         ///  </ Summary> 
        public DeptMemento DeptMemento { GET ; SET ;}
         public MementoCaretaker () 
        { 
            DeptMementoDictionary = new new the Dictionary < String , DeptMemento> (); 
        } 

    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Dept> dept = new List<Dept>()
            {
                new Dept() { DeptName="技术部", Number = "15987487456"},
                new Dept() { DeptName="人事部", Number = "15987487457"},
                new Dept() { DeptName="产品部", Number = "15,987,487,458 " } 
            }; 

            #region single backup point // phone list of sponsors 
            DeptBackOriginator mobileOriginator = new new DeptBackOriginator (the dept); 
            mobileOriginator.Show (); // create and save a memo memo object 
            MementoCaretaker Manager = new new MementoCaretaker (); 
            manager.DeptMemento = mobileOriginator.CreateMemento (); /// creating the first backup point 
            manager.DeptMementoDictionary.Add ( " One " , mobileOriginator.CreateMemento ()); // change sponsors contact list 
            Console.WriteLine (

            

            

            

            " ---- removes the last contact -------- " ); 
            mobileOriginator.DeptList.RemoveAt ( 2 ); 
            mobileOriginator.Show (); 

            /// create a second backup point 
            Thread.Sleep ( 1000 ); // wait 10 seconds 
            manager.DeptMementoDictionary.Add ( " Two " , mobileOriginator.CreateMemento ()); 

            // restored to its original state 
            Console.WriteLine ( " ------- restore your contact list --- --- " ); 
            mobileOriginator.RestoreMemento (manager.DeptMemento); 
            mobileOriginator.Show (); 
            #endregion 

            #region multiple backup points
            Console.WriteLine ( " ------- ------ output two backup point " );
             /// / output backup point 
            the foreach ( var Item in manager.DeptMementoDictionary) 
            { 
                mobileOriginator.RestoreMemento (item.Value ); 
                mobileOriginator.Show (); 
            } 
            Console.WriteLine ( " ------- restore a backup to the first point ------ " ); 
            mobileOriginator.RestoreMemento (manager.DeptMementoDictionary [ " One " ]) ; 
            mobileOriginator.Show (); 
            Console.WriteLine ( "------- restore the backup to the second point ------ " ); 
            mobileOriginator.RestoreMemento (manager.DeptMementoDictionary [ " Two " ]); 
            mobileOriginator.Show (); 
            #endregion 

            Console.Read () ; 
        } 
    } 
}

 

Guess you like

Origin www.cnblogs.com/May-day/p/11684589.html