类图
using System; public class NiceGirl { private string _Name; private string _Tall; private string _SkinColor; private string _HairColor; private string _Weight; private string _Colthes; private string _Shoes; private string _Bag; public NiceGirl() { } public NiceGirl(string name,string tall,string skincolor,string haircolor,string weight,string clothes,string shoes,string bag) { _Name = name; _Tall = tall; _SkinColor = skincolor; _HairColor = haircolor; _Weight = weight; _Colthes = clothes; _Shoes = shoes; _Bag = bag; } public string Name { get { return _Name; } set { _Name = value; } } public string Tall { get { return _Tall; } set { _Tall = value; } } public string SkinColor { get { return _SkinColor; } set { _SkinColor = value; } } public string HairColor { get { return _HairColor; } set { _HairColor = value; } } public string Weight { get { return _Weight; } set { _Weight = value; } } public string Colthes { get { return _Colthes; } set { _Colthes = value; } } public string Shoes { get { return _Shoes; } set { _Shoes = value; } } public string Bag { get { return _Bag; } set { _Bag = value; } } public DressStyle CreateCurrentDressStyle(string stylename) { return new DressStyle(stylename, _Tall, _SkinColor, _HairColor, _Weight, _Colthes, _Shoes, _Bag); } public void ApplyDressStyle(DressStyle dressstyle) { _Bag = dressstyle.Bag; _Colthes = dressstyle.Clothes; _HairColor = dressstyle.HairColor; _Shoes = dressstyle.Shoes; _SkinColor = dressstyle.SkinColor; _Tall = dressstyle.Tall; _Weight = dressstyle.Weight; } public override string ToString() { string decorationList = string.Empty; decorationList += "Name:" + this.Name + "/n" + "Tall:" + this.Tall + "/n" + "SkinColor:" + this.SkinColor + "/n" + "HairColor:" + this.HairColor + "/n" + "Weight:" + this.Weight + "/n" + "Clothes:" + this.Colthes + "/n" + "Shoes:" + this.Shoes + "/n" + "Bag:" + this.Bag; return decorationList; } }
待备忘的对象
using System; public class DressStyle { string _Tall, _SkinColor, _HairColor, _Weight, _Clothes, _Shoes, _Bag,_StyleName; public DressStyle(string stylename,string tall, string skincolor, string haircolor, string weight, string clothes, string shoes, string bag) { _Tall = tall; _SkinColor = skincolor; _HairColor = haircolor; _Weight = weight; _Clothes = clothes; _Shoes = shoes; _Bag = bag; _StyleName = stylename; } public string StyleName { get { return _StyleName; } } public string Tall { get { return _Tall; } } public string SkinColor { get { return _SkinColor; } } public string HairColor { get { return _HairColor; } } public string Weight { get { return _Weight; } } public string Clothes { get { return _Clothes; } } public string Shoes { get { return _Shoes; } } public string Bag { get { return _Bag; } } }
备忘录对象
using System; using System.Collections; public class DressStyleBox { private Hashtable _DressStyleTable; public DressStyleBox() { _DressStyleTable = new Hashtable(); } public void AddDressStyle(DressStyle dresstyle) { _DressStyleTable.Add(dresstyle.StyleName, dresstyle); } public DressStyle GetDressStyle(string stylename) { return _DressStyleTable[stylename] as DressStyle; } }
备忘录池
using System; using System.Collections.Generic; using System.Text; namespace MementoPattern { class Program { static void Main(string[] args) { DressStyleBox dsb = new DressStyleBox(); NiceGirl avaril = new NiceGirl("Avaril", "170CM", "白色", "黄色", "50KG", "背心", "板鞋", "双肩包"); dsb.AddDressStyle(avaril.CreateCurrentDressStyle("Ski-Girl")); NiceGirl lucy = new NiceGirl(); lucy.Name = "Lucy"; lucy.ApplyDressStyle(dsb.GetDressStyle("Ski-Girl")); Console.Write(lucy.ToString()); Console.ReadKey(); } } }
调用者